These are chat archives for Automattic/mongoose

28th
Aug 2018
Johnny Wu
@wxs77577
Aug 28 2018 00:33
Hi, the related field is user_id, but how can I retrive the related object as user?
Kev
@lineus
Aug 28 2018 00:50
@wxs77577 do you mean .populate()?
Maurizio Bellemo
@mbellemo_twitter
Aug 28 2018 06:47
@lineus would you solve the problem of the aggregate like I did with a post hook? How would you do it?
Johnny Wu
@wxs77577
Aug 28 2018 12:44
@lineus maybe, but if I use populate, the populated key name must be same as the field in database, but I use user_id as the database field name, then I wan to populate related object as user.
ratnesh2581990
@ratnesh2581990
Aug 28 2018 13:45
I am having problem in updating my two field brandSerialNumber and brand and also i want to add a new key with name delivaryquantity in the below the marked array field in mongo db here is my document
{ "_id": { "$oid": "5b811a01372fc4000414a62c" }, "cartobject": [ { "_5b6550bfc6def200042acc7d_Avira_30": { "_id": "5b6550bfc6def200042acc7d", "sku": "9", "brandSerialNumber": "2", "quantity": 1, "productName": "dummi 2", "brand": "Avira", "pack": { "packvalue": "30", "totpack": "30", "innerpack": "1", "packcharges": "0" }, "price": 3000, "brandPrice": "3100", "offerMaxKgLimit": 60, "qtyInKg": 30, "offer": "10", "offerPrice": 3100, "staticOfferPrice": 3100, "offerId": "5b811902372fc4000414a62a", "fullOffer": { "_id": "5b811902372fc4000414a62a", "offerstartdate": "2018-08-16T00:00:00.000Z", "offerenddate": "2018-08-26T00:00:00.000Z", "productname": "dummi 2", "offerproductid": "5b6550bfc6def200042acc7d", "productbrand": "Avira", "pack": "30", "offerinfo": "testing ", "priceoffer": "10", "maxlimituser": "60", "maxlimituserunit": "Kg", "startdatemiliseconds": 1534377600000, "enddatemiliseconds": 1535241600000, "offersizeunit": "qtl", "offersize": 2, "offerremaingquantity": 0, "priceofferunit": "rupees", "priceofferapplied": "perpack", "filepath": "public/uploads/offer/1535187138151-pexels-photo-433527.jpeg", "filename": "1535187138151-pexels-photo-433527.jpeg", "__v": 0 }, "discountOnQuantity": 10, "staticDiscountOnQuantity": 10, "transportCharge": 8, "transportRate": 8, "totalItemPrice": 936 } } ], "orderdate": { "$date": "2018-08-25T14:27:36.000Z" }, "customername": "1111", "brokername": "for testing", "amount": 928, "orderid": 1535187396908, "ordernote": "", "userid": "5b50375de1b8d4000485ffd6", "transportcharge": 8, "ordermilisecond": 1535187456640, "customernumber": null, "brokernumber": null, "__v": 0 }
I have tried all the things on mongo db official document and also on this platform but could not find the proper answer. here is the query which i have tried but could not find the proper answer
Order.update(query, {'$set': { 'cartobject._5b6550bfc6def200042acc7d_Avira_30.$.brand': 'Udan', 'cartobject._5b6550bfc6def200042acc7d_Avira_30.$.brandSerialNumber': 1 }}, function(err) { if(err){ res.json({ 'success': false, 'message': err }); } else { res.json({ 'success': true, 'message': 'success' }); } });
ratnesh2581990
@ratnesh2581990
Aug 28 2018 13:51
please if you have solution of my problem then please help me
I am in big problem
Kev
@lineus
Aug 28 2018 13:53
@mbellemo_twitter I would be a bit hesitant to run an aggregate query and an update query every time I ran findOneAndUpdate, but if that's what your needs dictate, then I don't see why not.
@wxs77577 have you looked at creating a virtual to populate with?
@ratnesh2581990 are the fields that aren't updating defined in your schema?
ratnesh2581990
@ratnesh2581990
Aug 28 2018 13:58
@lineus Those fields are part of my array field name cartobject
Kev
@lineus
Aug 28 2018 14:02
@ratnesh2581990 you can share code here that's a little easier to read by adding 3 backticks on a line before the code and 3 backticks on a line after the code. Like this:
```
code
```
can you share your schema?
Kev
@lineus
Aug 28 2018 14:27
sorry, I forgot to mention shift-enter to create a newline without sending the message :)
ratnesh2581990
@ratnesh2581990
Aug 28 2018 14:28
let mongoose = require('mongoose');
let orderSchema = mongoose.Schema({
    orderid: {
        type: Number,
        required: true
    },
    customername: {
        type: String,
        required: true
    },
    brokername: {
        type: String,
        required: true
    },
    customernumber: {
        type: Number
    },
    brokernumber: {
        type: Number
    },
    amount: {
        type: Number,
        required: true
    },
    transportcharge: {
        type: Number,
        required: true
    },
    orderdate: {
        type: Date,
        required: true,
        default: Date.now
    },
    ordermilisecond: {
        type: Number,
        required: true
    },
    ordernote: {
        type: String
    },
    cartobject:{
        type: Array
    },
    userid: {
        type: String,
        required: true
    }
});

let Order = module.exports = mongoose.model('Order', orderSchema);
@lineus here is my schema of db for mongoose
Kev
@lineus
Aug 28 2018 14:31
ok, so the type of the cartobject path is an Array of mixed values, so that likely isn't the problem. I'll go back and look at your update query.
@ratnesh2581990 what is the value of query in your call to Order.update(?
ratnesh2581990
@ratnesh2581990
Aug 28 2018 14:35
let query = {_id:req.params.id};
@lineus I have search with _id parameter in table
Kev
@lineus
Aug 28 2018 14:45
@ratnesh2581990 and are you getting an error from mongodb? or is just not updating the doc?
Maurizio Bellemo
@mbellemo_twitter
Aug 28 2018 15:15
@lineus I need to make the sum of the subdocuments values and sort by them. How else would you do it?
@mbellemo_twitter I'd probably create a static method on the schema that calls the aggregate and update. By doing this in a pre/post hook attached to findOneAndUpdate, you're saying that every call on that model to findOneAndUpdate will require this aggregation. I would think at some point you're going to want to findOneAndUpdate without the expense of aggregate.
Kev
@lineus
Aug 28 2018 15:23
but that's all conjecture, if it works for you based on your specific requirements, that's all that matters :+1:
Maurizio Bellemo
@mbellemo_twitter
Aug 28 2018 15:39
@lineus interesting. Does it mean that if I create a static method, I have to call it whenever I perform an edit of the object (add, removing, editing a deal) which would influence the total?
Kev
@lineus
Aug 28 2018 15:40
yeah, instead of calling Model.findOneAndUpdate(), you would call that static method which would contain the aggregate call and ideally one update call to add the aggregated total to the doc in addition to any other paths that you wanted to update.
Maurizio Bellemo
@mbellemo_twitter
Aug 28 2018 15:48
@lineus wow! That is a really good idea.
hillct
@hillct
Aug 28 2018 20:01
@lineus I too was leaning toward the array overwrite approach. While certainly expedient, it’s rather inelegant. Hard to say which of thoe criteria should win out. Thanks.