问题
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