Query across collections using join

Is there any option to join two collections in the same database to pull some data. Join on some field or some text etc…

Hi there,

You can use $lookup to achieve joins on collections.

You can read about it’s use here:

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

1 Like

how can we make $lookup work with a manual reference on one side and an ObjectId on the other side? Thanks.

Hi @log_blackhole,

Please elaborate the query so that I can help you more precisely.
Also, please take a look at our documentation:

You can also check the following links for more details on using $lookup with Object Id field:

Please let me know, if you have any questions.

Thanks,
Sonali

1 Like

Hi @Sonali_Mamgain
The question is very simple. MongoDB encourages using manual references rather than DBRef, meaning that references are stored as simple strings. Now, the $lookup operator does not seem to work with one manual reference and ObjectId like this:
{$lookup: {from:‘users’, localField: ‘users_id, foreignField: ‘_id’, as: ‘myUsers’} },
There is a post on stackoverflow saying that using foreignField: ’ _id.str’ should resolve this issue in version 4 which does not. Its strange that this is not working out of the box and might require additional projection operations as MongoDB 's preference is to use manual reference by default. Thanks.