These are chat archives for Automattic/mongoose

28th
Sep 2018
Kev
@lineus
Sep 28 2018 10:01
@mbellemo_twitter calling update on the document directly doesn't require a query, mongoose internally attaches the query object with the _id field for that document for you. The problem is though, that update() doesn't return the updated document either. You can either use Model.findOneAndUpdate() with your query and the new option set to true, or mutate the document that you've already retrieved locally in node and call feedback.save() both of which will return the newly saved document to their callbacks/promises.
ratnesh2581990
@ratnesh2581990
Sep 28 2018 11:29

Hello @lineus i am trying to use populate like this

Order.
    findById(req.params.id).
    populate('userid').
    populate({ path: 'cartobject', populate: { path: 'productid' } }).
      sort({_id: 'desc'}).
      exec(function (err, order) {
        if(err) {
            console.log(err);
        } else {
            console.log(order);
            res.render('order_single.hbs', {
                pageTitle: 'Order',
                order:order
            });
        }
      });

by referring example from https://mongoosejs.com/docs/populate.html
but every time it will give productid value null

My schema for order table and ordermeta table are as follows
Order table

let mongoose = require('mongoose');
var Schema = mongoose.Schema;
let orderSchema = Schema({
    orderid: {
        type: Number,
        required: true
    },
    customername: {
        type: String,
        required: true
    },
    brokername: {
        type: String,
        required: true
    },
    customernumber: {
        type: Number
    },
    user_id: {
        type: String,
    },
    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
    },
    walletamountused: {
        type: Number,
        required: true
    },
    ordernote: {
        type: String
    },
    cartobject:[{ type: Schema.Types.ObjectId, ref: 'Ordermeta' }],
    userid: {
        type: Schema.Types.ObjectId, ref: 'User',
        required: true
    }
});

let Order = module.exports = mongoose.model('Order', orderSchema);

Order Meta table

let mongoose = require('mongoose');
var Schema = mongoose.Schema;
let ordermetaSchema = Schema({
    orderid: {
        type: Number, ref: 'Order',
        required: true
    },
    productid: {
        type: Schema.Types.ObjectId, ref: 'Product',
        required: true
    },
    brandid: {
        type: Schema.Types.ObjectId, ref: 'Brand',
        required: true
    },
    quantity: {
        type: Number,
        required: true
    },
    packdetails: {
        type: Object
    },
    price: {
        type: Object
    },
    deliveredQty: {
        type: Number,
        required: true
    },
    balanceQty: {
        type: Number,
        required: true
    },
    qtyInKg: {
        type: Number,
        required: true
    },
    offer:{
        type: String,
        required: true
    },
    offerid: {
        type: Schema.Types.ObjectId, ref: 'Offer',
        required: true
    },
    discountOnQuantity: {
        type: Number
    }
});

let Ordermeta = module.exports = mongoose.model('Ordermeta', ordermetaSchema);

So can you help me in finding where i am doing wrong

Hitesh Joshi
@evoxtorm
Sep 28 2018 11:36
hey how to index the old collection in database. Does this index is only applicable for new data or it is also indexed for the old data ???
Kev
@lineus
Sep 28 2018 11:54
@ratnesh2581990 I don't see anything wrong with what you've got there, can you share a set of documents, from each collection that you think should work, ie one document from 'Order' and the referenced docs from 'Ordermeta' and 'Product' as they are stored in the db (assuming it's test data)?
ratnesh2581990
@ratnesh2581990
Sep 28 2018 11:57
ok
Kev
@lineus
Sep 28 2018 11:57
@evoxtorm when you create an index on a collection, by default it will build the index in the foreground for existing docs ( ie blocking reads/writes ), and then subsequently, new docs will be added in a non-blocking way. Depending on the number of docs in the collection and the specific index, this operation can take a while.
ratnesh2581990
@ratnesh2581990
Sep 28 2018 12:14
@lineus I have found the solution of this problem problem is in my db now it is working fine but can you tell me is it possible to run query like this
Order.
    findById(req.params.id).
    populate('userid').
    populate({ path: 'cartobject', populate: { path: 'productid' }, populate: { path: 'brandid' }, populate: { path: 'offerid' }, }).
      sort({_id: 'desc'}).
      exec(function (err, order) {
        if(err) {
            console.log(err);
        } else {
            console.log(order.cartobject);
            res.render('order_single.hbs', {
                pageTitle: 'Order',
                order:order
            });
        }
      });
Kev
@lineus
Sep 28 2018 15:01

that's probably a syntax error @ratnesh2581990 as you're declaring the same key 'populate' more than once in an object. The path string can be a space separated string with multiple path names though.

populate({ path: 'cartobject', populate: { path: 'productid brandid offerid' } })

should work.

looks like it's not a syntax error, each subsequent populate property overwrites the previous ones:
issues: node
> let x = { path: 'cartobject', populate: { path: 'productid' }, populate: { path: 'brandid' }, populate: { path: 'offerid' }, }
undefined
> x
{ path: 'cartobject', populate: { path: 'offerid' } }
> issues: