这是我的 node.js 后端的代码:
app.get('/getpossibleconnections', auth, function(req, res){
if (req.authenticated == false){
res.send("Your session has expired.");
} else {
User.aggregate([
{ $match: { _id: { $nin: req.decoded.username.connections } } },
{ $sample: { size: 10 } },
]).next(function(err, docs) {
console.log("horray");
});
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试按照此处的建议替换next()为toArray()和:each()
http://mongodb.github.io/node-mongodb-native/2.0/tutorials/aggregation/
但是,我每次都会收到相同的错误:
TypeError: User.aggregate(...).next is not a function
为什么我无法使用任何类型的函数迭代返回的文档?
是因为User不是合集吗?
尝试这个:
var cursor = User.aggregate([
{ $match: { _id: { $nin: req.decoded.username.connections } } },
{ $sample: { size: 10 } },
]).cursor().exec();
cursor.each(function(err, doc) {
//do something with doc
});
Run Code Online (Sandbox Code Playgroud)
Mongoose 处理光标对象的聚合的方式与您在链接中发布的 Mongodb-native 不同。更多信息请参见:mongoose 聚合游标文档
| 归档时间: |
|
| 查看次数: |
6038 次 |
| 最近记录: |