【发布时间】:2015-12-28 21:17:14
【问题描述】:
文档:
{
"_id" : ObjectId("560c24b853b558856ef193a3"),
"name" : "Karl Morrison",
"pic" : "",
"language" : ObjectId("560c24b853b558856ef193a2"),
"cell" : 1,
"local" : {
"email" : "karl.morrison@instanty.se",
"password" : "12345"
},
"sessions" : [
{
"id" : ObjectId("560c24b853b558856ef193a5")
}
]
}
这行得通:
yield new Promise(function (resolve, reject) {
users.col.aggregate([
{
$match: {
'name': 'Karl Morrison'
}
}
],
function (err, res) {
console.log('err ' + err);
console.log('res ' + JSON.stringify(res)); // <-- echos the object retrieved
if (err === null)
resolve(res);
reject(err);
});
});
这不起作用:
yield new Promise(function (resolve, reject) {
users.col.aggregate([
{
$match: {
'_id': '560c24b853b558856ef193a3' // <-- _id of the user
}
}
],
function (err, res) {
console.log('err ' + err);
console.log('res ' + JSON.stringify(res));
if (err === null)
resolve(res);
reject(err);
});
});
.col 访问本机 mongodb 对象(否则使用 co-monk)。所以我是手动做的。然而,这不起作用。我怀疑我没有将 id 十六进制字符串转换为 ObjectId。无论我尝试什么都没有效果。
【问题讨论】:
-
_id必须是ObjectId。 Monk 在与.aggregate()一起使用时无法从字符串中为您转换类型,因此您需要自己转换类型。 -
MongoDB - Aggregate $match on ObjectId 的可能重复项。基本相同的原因是“自己投射”,因为两个框架都使用相似的代码进行自动投射,但都不适用于
.aggregate()。 -
@BlakesSeven 确实是,谢谢,找了好久(可能是猫鼬没找到)
标签: mongodb