0

I have these two 3 models User, Product and Orders and are also has references to each other.

My Orders Schema:

const orderSchema = Schema({

buyerId:{
    type: Schema.Types.ObjectId,
    ref: 'User'
},
totalAmount: {
    type: Number,
    required: [true, "description is required"]
},
    createdOn: {
    type: Date,
    default: new Date()
},
    items:[{type:Schema.Types.ObjectId, ref: 'Product'}]})

I'm trying to use populate() like this:

    Order.find()
    .populate('buyerId')//Reference to User Model
    .populate('items')// Reference to Product Model
    .exec(function (err, result){

        console.log(result);// RETURNS ONLY buyerId populated
        console.log(result.buyerId.name);//Successfully references to my User model and prints name
        console.log(result.items);//Prints Undefined

    })

You can see my console log above and what it returns is only the populated buyerId(only got the reference from my User model)

Seems like my populate('items') doesnt work at all. The items field contains array of IDs, where the IDs are those of products. I need to reference to User and Product both. I'm just following the documentation in mongoose, I don't know what I might be doing wrong.

6
  • i think lookup is better , if you want I create query Commented Sep 12, 2021 at 13:32
  • Yes can you help me with that please Commented Sep 12, 2021 at 13:36
  • If you don't put .populate('items'), does items contain a list of ids or still undefined ? Because, then there might be something wrong with the way you add a new product inside your order, or with the models. Commented Sep 12, 2021 at 13:50
  • @jeremynac if I remove it and just leave the populate buyer Id I can reference and get the data from the User collection Commented Sep 12, 2021 at 13:56
  • 1
    Ok, if you just put .populate('items'), are items populated ? Commented Sep 12, 2021 at 14:01

1 Answer 1

1

use aggregate

Order.aggregate([
    { $match:{ user:"sample_id"
     }
    },
    {$lookup:{
        from:'users', // users collection name
        localField:'buyerId',
        foreignField:'_id',
        as:'buyerId'
    }},
    {
        $lookup:{
            from:'items', //items collection name
            localField:'items',
            foreignField:'_id',
            as:'items'
        }
    },

])
Sign up to request clarification or add additional context in comments.

13 Comments

How do I reference to those as: fields and as: buyerId? How do I output the result? I connected .then(result) at the end of your code and console log it but just got the documents from Order collections
lookup as field put populated data as field name we defined in lookup as value of as, from is collection name of each schema you want populate , I don't know the collection names , so I just make query to change it as your environment
Oh I think I'm getting it now. Thank you, I'll update here.
Thank you problem solved. I've been dealing with this for hours.
you could add another stage match now I will update my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.