These are chat archives for Automattic/mongoose

19th
Sep 2018
ratnesh2581990
@ratnesh2581990
Sep 19 2018 06:05
@lineus Here is my schema for order collection
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
    },
    ordernote: {
        type: String
    },
    cartobject:{
        type: Array
    },
    userid: {
        type: Schema.Types.ObjectId, ref: 'User',
        required: true
    }
});

let Order = module.exports = mongoose.model('Order', orderSchema);
Here is the schema for product collection
let mongoose = require('mongoose');
let productSchema = mongoose.Schema({
    producttitle: {
        type: String,
        required: true
    },
    productbody: {
        type: String,
    },
    sku: {
        type: String,
         required: true
    },
    unit: {
        type: String,
        required: true
    },
    productbrand: {
        type: Array
    },
    brandprice: {
        type: Object,
    },
    brandpricediff: {
        type: Object,
    },
    regularprice: {
        type: Number,
    },
    filepath: {
        type: String,
        trim: true
    },
    filename: {
        type: String
    },
    ratechangedate:{
        type: String
    },
    creationdate: {
        type: Date,
        required: true,
        default: Date.now
    },
    datemiliseconds: {
        type: Number,
        required: true
    },
    modifieddate: {
        type: Date
    }
});

let Product = module.exports = mongoose.model('Product', productSchema);
Kev
@lineus
Sep 19 2018 10:29

@ratnesh2581990 I see now. You've defined the cartObject path in your orderSchema as an Array of Mixed and each embedded document has an arbitrary shape ( ie, different top level keys: _5b93848db79f8100048948c1_PARINDA_30 ).

Mongoose requires an explicitly defined path to populate. So you'll need to either

Change the shape of the embedded document to not have dynamically defined keys,

or

1) do the find to retrieve the order docs,
2) then construct a populate object based on the top level key of the objects in the cartObjects array,
3) finally, populate each order doc with paths created in step 2

here's an example of the second way

It would be nice if we could provide populate with a path containing a glob-like-syntax string to identify dynamic paths. I'll consider putting in a PR/Issue for that in addition to the bug I found while writing this.
Kev
@lineus
Sep 19 2018 12:48

With regard to the first way ( the way that I would choose ), your embedded documents within the cartObjects array already have the _id of the product stored in the object, so instead of adding that id to the top level key of the document, I would have an explicitly defined subSchema for cartObjects, maybe something like:

const subSchema = new Schema({
  product: {
    _id: mongoose.Schema.Types.ObjectId,
    parinda: Number,
    ... // rest of the properties
  }
}, { _id: false });

then your populate call becomes Order.find({}).populate({ path: 'cartObjects.product._id', model: 'product' })'

With regard to the second way this won't result in any additional queries to the db, it just means you have to explicitly declare the paths you want to populate before you actually call populate.

le-dorje
@le-dorje
Sep 19 2018 20:22
Hi. I'm trying to wrap my head around MongooseMaps. I understand the socialMediaHandles example, but the one at the top of the page (https://mongoosejs.com/docs/schematypes.html) confuses me a little. I don't understand how map and mapOfStrings in the schema are tied together below, m.map = new Map([['key', 'value']]); Is it just looking for the very next map definition?
Thanks for any help :)