在猫鼬中,使用 Find 我可以做类似下面的事情。
var activeUserQuests = ['51a02dade4b02780aeee5ab7', '51a0a40ce4b0cbb4519a8f69'];
Quest.find({_id: { $in: activeUserQuests} }, function(err, doc){
callback(doc);
});
我想做的是做类似的事情,但是通过 MapReduce 函数。目前我有以下。
var o = {};
o.map = function() {
emit(this.problem.id, {problem:this.problem.title,quest:
{
title:this.title,
created_date: this.created_date,
updated_date: this.updated_date,
author: this.author,
description: this.description
}
});
}
o.reduce = function(previous, current) {
var array = [];
var res = {quests:array};
current.forEach(function (v) {
res.quests.push(v);
});
return res;
}
并像这样调用方法
findAll: function(callback){
Quest.mapReduce(o, function(error, model, stats) {
callback(model);
});
}
我的 JSON 看起来像这样:
[
{
"_id": "51a02dade4b02780aeee5ab7",
"title": "Foo",
"created_date": 2342423423423,
"updated_date": 23424234234233
},
///This one should not show up...
{
"_id": "99s8d7f9sdf79d9f7ds8f7",
"title": "Bar",
"created_date": 2342423423423,
"updated_date": 23424234234233
},
{
"_id": "51a0a40ce4b0cbb4519a8f69",
"title": "Bazz",
"author": "sdfsdfsf",
"created_date": 2342423423423,
"updated_date": 2342423423423,
"description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
}
]
这很好用,但是我只想显示通过数组输入的项目,就像我对 find() 所做的那样。本质上,我想做的只是……基于任意数量的项目和制作人,只展示和分组我自己的。我假设通过提供 ID 列表,如果有很多项目,我可能会加快返回速度。