Query to join multiple collections

165 views
Skip to first unread message

Adi

unread,
Mar 13, 2018, 5:49:41 PM3/13/18
to mongodb-user
Hi 

i'm working with a database and need to find a way to create a query to join multiple collections.

In the collections which need joining there are fields which contain arrays of pointers

Example:

userid: dh2892938d9
username: exampleuser
carparts:  array(
    array(
              collection-name: "name of another collection"
              id: "id of row in other collection"
     ), array(
              collection-name: "name of another collection"
              id: "id of row in other collection"
     ), array(
              collection-name: "name of another collection"
              id: "id of row in other collection"
     ),
)

There are also often pointers in the secondary collection pointing to another collection

At the moment I'm running multiple queries by following each pointer but it's a lot of connections. 
Is there a way of querying everything in one query using joins somehow?

Thanks for any help
Adi

Kevin Adistambha

unread,
Mar 28, 2018, 9:51:13 PM3/28/18
to mongodb-user

Hi Adi

Is there a way of querying everything in one query using joins somehow?

MongoDB since version 3.2 provides the $lookup operator in the aggregation framework that does a left outer join of two collections.

Having said that, it looks like your MongoDB schema design was inherited from a relational database. Is this correct? Typical MongoDB schema design would embed subdocuments and denormalize the data, so that the application can find the most common case data it needs with a single find() query.

For more information about MongoDB schema design, please see:

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages