有收藏
city: {"_id", "name"}
company: {"_id", "name", "cityID"}
comments: {"_id", "text", "companyID"}
您必须选择对某个城市的公司的最后 10 条评论。
现在我先选择城市中所有公司的_id,然后在_id上收到10条评论,代码如下:
$ db-> execute ('function () {
var result = {};
var company = [];
result.company = [];
db.company.find ({"city": "msk"}, {"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 ({"createTime": -1}). limit (10). forEach (function (a) {
result.comments [a._id] = Object.deepExtend (a, result [a._id]);
comments.push (a._id);
});
return result;
} ');
Esti 是做我需要的更好的选择吗?提前感谢您的建议!