We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



93.7k
Accepted
answer

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

Good luck! :)