Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Mongodb Query Using ODM

Hi,

New to mongodb. I have a question on how to make a mongodb query that involves 2 collections.

Below is my collections.

People
_id 
name
email

Cars
_id
model
year
peopleId

I need to list down all the people records. For each people record, i need to return the number of cars they own.

Current Solution

Involves 2 queries. First query to select all people record. Using a for loop, Second query to count and retrieve the cars records for each people

I am looking for a better solution. A single query that can achieve the above. Any advice appreciated.



66.0k
Accepted
answer

Hello, can you check this example and answer here: http://stackoverflow.com/questions/17279895/mongodb-aggregation-count-over-a-relation I think its the same scenario you have.

Good luck! :)