mpo*_*zil 19 mongoose mongodb node.js
我在shell中构造了我想要的查询但是在Mongoose中编写它时遇到了麻烦.
db.commentstreams.group({ key: { page_id: true }, reduce: function(obj,prev) { prev.num_comments += obj.num_comments }, initial: { num_comments: 0 } })
Run Code Online (Sandbox Code Playgroud)
我对Mongoose语法感到有点困惑; 也许有人可以对这一点有所了解.非常感谢.
and*_*yuk 14
根据这篇文章:
Model.find({}, [fields], {'group': 'FIELD'}, function(err, logs) {
...
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,我们似乎缺乏这方面的文档.
小智 13
MapReduce应该是正确的方法,但是如果你没有分片,你的查询不会导致超过10k的记录就可以了.
您可以通过moongoose访问任何node-mongodb-native函数,所以它看起来像这样:
var group = {
key: {},
cond: {item_id: item_id},
reduce: function(doc, out) {
out.count++;
out.total += doc.value;
},
initial: {
total: 0,
count: 0
},
finalize: function(out) {
out.avg = out.total / out.count;
}
};
Item.collection.group(group.key, group.cond, group.initial, group.reduce, group.finalize, true, function(err, results) {
console.log('group results %j', results);
});
Run Code Online (Sandbox Code Playgroud)
node-mongodb-native source:https: //github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/collection.js#L1165
MongoDB小组doc:http: //www.mongodb.org/display/DOCS/Aggregation#Aggregation-Group
归档时间: |
|
查看次数: |
19175 次 |
最近记录: |