Search on multiple collections in MongoDB

前端 未结 7 1975
自闭症患者
自闭症患者 2020-12-01 01:28

I know the theory of MongoDB and the fact that is doesn\'t support joins, and that I should use embeded documents or denormalize as much as possible, but here goes:

7条回答
  •  青春惊慌失措
    2020-12-01 01:50

    This answer is outdated. Since version 3.2, MongoDB has limited support for left outer joins with the $lookup aggregation operator

    MongoDB does not do queries which span multiple collections - period. When you need to join data from multiple collections, you have to do it on the application level by doing multiple queries.

    1. Query collection A
    2. Get the secondary keys from the result and put them into an array
    3. Query collection B passing that array as the value of the $in-operator
    4. Join the results of both queries programmatically on the application layer

    Having to do this should be rather the exception than the norm. When you frequently need to emulate JOINs like that, it either means that you are still thinking too relational when you design your database schema or that your data is simply not suited for the document-based storage concept of MongoDB.

提交回复
热议问题