-
Type: Question
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Querying
-
Labels:None
-
Environment:PHP driver
Hello!
There are collections
city:
{"_id", "name"}company:
{"_id", "name", "cityID"}comments:
{"_id", "text", "companyID"}You must select the last 10 comments on companies of a certain city.
Now I select _id first of all companies in the city, and then the 10 comments received on _id
Here's the code:
$ db-> execute ('function () {
var result = {};
var company = [];
result.company = [];
db.company.find (
,
{"title": 1, "_id": 1}). forEach (function (a) {
result.company [a._id] = Object.deepExtend (a, result [a._id]);
company.push (a._id);
});
var comments = [];
result.comments = [];
db.comments.find ({"company": {"$ in": company}}). sort (
). limit (10). forEach (function (a)
{ result.comments [a._id] = Object.deepExtend (a, result [a._id]); comments.push (a._id); }); return result; }');
Esti's a better option to do what I need?
Thanks in advance for your advice!