【发布时间】:2016-05-04 06:05:18
【问题描述】:
我有以下几行:
Score.find({gameId : gameId}).exec(function(err,obj){
console.log("find length : " + obj.length);
});
Score.aggregate([
{$match: {gameId: gameId}}
], function (err, result) {
console.log('result is : ' + JSON.stringify(result));
console.log('is there any error : ' + err);
});
这些行的输出是
result is : []
is there any error : null
find length : 1
我不明白,为什么聚合的“匹配”方法不能按预期工作 - 查找所有匹配属性的文档。我添加了具有相同“正文”的Score.find,以查明该文档是否确实存在并且确实存在。
PS : {gameId: gameId} - 第一个 gameId 是属性名称,第二个是我要查找的 ID 字符串值。
PS2:流利的 api 具有相同的结果:
Score.aggregate().match({gameId: gameId}).exec(function (err, result){
console.log('result is : ' + JSON.stringify(result));
console.log('is there any error : ' + err);
});
【问题讨论】:
-
你是否也尝试过流畅的API方法
Score.aggregate().match({gameId: gameId}).exec(callback)? -
@chridam - 现在尝试,结果相同
-
你试过把它转换成猫鼬id吗?
mongoose.Types.ObjectId(gameId)或new ObjectId(gameId)原生 -
@styopdev - 这是工作
mongoose.Types.ObjectId(gameId),谢谢!请写出来作为答案,我可以接受它
标签: node.js mongodb mongoose aggregation-framework