Reputation: 9664
Is it possible in a single query (or whatever the fastest way is) to get an array of values from one collection and get a bunch of docs with the results of that...
collection2.find({bands: 'the killers'}, {eventId: 1}).toArray(function(err, docs) {
if(docs !== []){
//get all the event ID's
var eventIds = [];
for(var i = 0; i < docs.length; i++){
eventIds.push(docs[i].eventId);
}
//make a search with all ID's
collection.find({ _id: {$in: eventIds} }).toArray(function(err, docs){
res.send(docs);
});
}
});
So here I am finding all the events where the killers are playing, then grabbing all the eventID's and getting the full event info with that.
I didnt want to combine the bands with the event info as the band list can get quite large. I am also aware that you can ignore certain fields with say {bands: 0} - would this effect the speed of the query if the bands array was large?
Upvotes: 0
Views: 80
Reputation: 11671
MongoDB does not do joins. Querying information from two collections requires 2 queries. Joins are done application-side, as in your example code. Your schema should be designed to make common and important operations easy and fast, accomplished with one indexed query. Less common and less important operations can be done with application-level joins, aggregation pipelines, or, if necessary, map/reduce. You might want to consider optimizing the schema to avoid a join for finding event information associated with a band, but I have little knowledge of your use case so I can't say what is the best course.
Omitting certain fields from the return of a query will speed up the query because the omitted information will not have to be sent from the server to the client.
Upvotes: 1