How to do inner joining in MongoDB?

后端 未结 4 831
醉梦人生
醉梦人生 2021-02-08 02:31

Is it possible to do SQL inner joins kind of stuff in MongoDB , well i know there is

$lookup

attribute in aggregation pipeline and

4条回答
  •  孤街浪徒
    2021-02-08 03:10

    As Tiramisu wrote this looks like schema issue.

    You can make a manual inner join, by removing documents where $lookup returned empty array.

    ....
    {$lookup... as myArray},
    {$match: {"myArray":{$ne:[]}}},
    {$lookup... as myArray2},
    {$match: {"myArray2":{$ne:[]}}},
    

    schema change

    I personally will go for schema update, like this:

    db.User.find({})
    {
       ID : 1,
       USER_NAME : "John",
       password : "pass"
       roles:[{ID : 1,  ROLE_NAME : "admin"}]
    }
    
    
    db.ROLE.find({})
    {
       ID : 1,
       ROLE_NAME : "admin"
    },
    

提交回复
热议问题