Reputation: 23
I have about ~15 collections (different providers with different structures of data) that got a few fields in common, let's say title, description and price.
I'm currently trying to implement a search functionality for my API using the common fields and I am able to do it for every collection individually.
Is it possible to make a query for all 15 collections at once using that common fields? The problem of doing them one by one is the performance issues ( I have to lean my results ) and the fact that there is pagination on top of it.
I was thinking of creating a shared collection with common fields is a little bit late I guess.
Upvotes: 2
Views: 3090
Reputation: 543
According to the mongo documentation, $lookup can join only one external collection.
What you could do is to combine userInfo and userRole in one collection, as provided example is based on relational DB schema. Mongo is noSQL database - and this require different approach for document management.
Please find below 2-step query, which combines userInfo with userRole - creating new temporary collection used in last query to display combined data. In last query there is an option to use $out and create new collection with merged data for later use.
db.sivaUser.insert(
{
"_id" : ObjectId("5684f3c454b1fd6926c324fd"),
"email" : "[email protected]",
"userId" : "AD",
"userName" : "admin"
})
//"userinfo"
db.sivaUserInfo.insert(
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"phone" : "0000000000"
})
//"userrole"
db.sivaUserRole.insert(
{
"_id" : ObjectId("56d82612b63f1c31cf906003"),
"userId" : "AD",
"role" : "admin"
})
db.sivaUserInfo.aggregate([
{$lookup:
{
from: "sivaUserRole",
localField: "userId",
foreignField: "userId",
as: "userRole"
}
},
{
$unwind:"$userRole"
},
{
$project:{
"_id":1,
"userId" : 1,
"phone" : 1,
"role" :"$userRole.role"
}
},
{
$out:"sivaUserTmp"
}
])
db.sivaUserTmp.aggregate([
{$lookup:
{
from: "sivaUser",
localField: "userId",
foreignField: "userId",
as: "user"
}
},
{
$unwind:"$user"
},
{
$project:{
"_id":1,
"userId" : 1,
"phone" : 1,
"role" :1,
"email" : "$user.email",
"userName" : "$user.userName"
}
}
])
Upvotes: 1
Reputation: 9268
There is no way to run a single query on multiple collections, what you can still do is, run all the queries parallely and wait for all he results to come, and then you can send the collective result in the response.
Code should look something like this:
var promises = [];
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
promises.push(Collection1.find({title : "title",desription : "description",...}).lean().exec());
Promise.all(promises).then(results=>{
// results[0] will have docs of first query
// results[1] will have docs of second query
// and so on...
// you can combine all the results here and send back in response
}).catch(err=>{
//handle error here
})
Upvotes: 4