How to join query in mongodb?

后端 未结 10 2232
情深已故
情深已故 2020-11-29 03:22

I have user document collection like this:

User {
   id:\"001\"
   name:\"John\",
   age:30,
   friends:[\"userId1\",\"userId2\",\"userId3\"....]
}
         


        
10条回答
  •  我在风中等你
    2020-11-29 04:10

    one kind of join a query in mongoDB, is ask at one collection for id that match , put ids in a list (idlist) , and do find using on other (or same) collection with $in : idlist

    u = db.friends.find({"friends": ? }).toArray()
    idlist= []
    u.forEach(function(myDoc) { idlist.push(myDoc.id ); } )
    db.friends.find({"id": {$in : idlist} } )
    

提交回复
热议问题