How to join the two table, it is in two different DB in mongodb

馋奶兔 提交于 2019-12-11 15:05:17

问题


I have the documents in following structure saved in my mongodb.

DB : DB1, Collection: Collection1

{
"_id":"111",
"studentID" : "1"
}

DB : DB2, Collection: Collection2

{
"_id":"111",
"studentID" : "1",
"login" : true
},
{
"_id":"333",
"studentID" : "2",
"login" : false
}

my question is i want join both table, then DB1-> Collection1 i have one document studentID is 1, but DB2-> Collection2 have 2 docs but i have to return one document which equal to DB1-> Collection1

mongodb version = 3.4

My Expected out

{
"_id":"111",
"studentID" : "1",
"login" : true
},

suppose single DB means, i can join but this case i don't have idea to write the query,please any one help out on this

I tried but it is not working, it is keep on loading...


var memberType;
db.Collection1.find({})
.forEach(function(doc){
        memberType = doc.studentID;
        if(memberType.length > 0){
            var records = db.getSiblingDB('DB2');
            records.Collection2.find({'studentID' : { '$in' : memberType }}).forEach(function(loc){
                print(loc); 
            })
        }
    })

来源:https://stackoverflow.com/questions/56102636/how-to-join-the-two-table-it-is-in-two-different-db-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!