- QUESTION -
How can I concatenate (or merge or union) the results of find() from two mongodb collections in Meteor JS? I preferably want to join them as a single cursor object, but fetching and converting to array would be acceptable. What should I use instead of
UNION
in the following code? Note: I don't need to remove repeated elements. A concatenation is enough.allLocations: function(){
location_set_1 = TableA.find({'loc':{$exists:1}},{loc:1,limit:300});
location_set_2 = TableB.find({'loc':{$exists:1}},{loc:1,limit:300});
combined_location_set = ??UNION??(location_set_1,location_set_2)
return combined_location_set;
}
A (n incomplete) solution may be the following but it will return an array. Instead I need to return a cursor object to the "helper" in Meteor:
a=TableA.find()
b=TableB.find()
c=_.extend({},a.fetch(),b.fetch())
// c=[].concat(a.fetch()).concat(b.fetch());
return c;
- ANSWER -
allLocations: function(){
var location_set_1 = TableA.find({'loc':{$exists:1}},{loc:1,limit:300}).fetch();
var location_set_2 = TableB.find({'loc':{$exists:1}},{loc:1,limit:300}).fetch();
var result = [];
for (i = location_set_1.length - 1; i >= 0; i --) {
result.push(location_set_1[i]);
}
for (i = location_set_2.length - 1; i >= 0; i --) {
result.push(location_set_2[i]);
}
return result;
}
0 comments:
Post a Comment