2

I'm facing some dificult to make this query with the MongoDB Aggregation Framrwork.

I have a order collection that has an array of objects ids from products example:

{
  _id: "5759b760aeacbfa420943d84",
  products: [
    "57718a2c9473f30ae88d1875",
    "57727d988d7e581809b454a1",
    "577bda7da756e2180507a944"
  ]
}

Here is an example of one document from my products ocllection:

{
  _id: "57718a2c9473f30ae88d1875",
  name: "Soap A"
}

How can I make an aggregation query in orders to get a list of contained products documents?

1 Answer 1

1

The short answer you can't. Aggregate works only on one collection.

However if you are using mongoose you can use query and '.populate' method

i.e

CollectionModel.find({}).populate('products').exec(function(err, items) {}) 

more details on populate http://mongoosejs.com/docs/populate.html

but that only applicable to nodejs application however language you are using have a look at some ORM frameworks for mongo

I hope that helps

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

2 Comments

But internally does Mongoose make another query to assemble that right?
@LeandroCurioso One way to get around this by either modifying Order model to include data that you need from product (de-normalize) and other one is to do lookups after aggregate is finished

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.