考虑我想显示以下文档:
{
"_id" : ObjectId("512bc95fe835e68f199c8686"),
"AuthorName": "dave",
"VirtualField" : "hardcoded_Value"
}
存储在 MongoDB 中的实际文档
{
"_id" : ObjectId("512bc95fe835e68f199c8686"),
"author": "dave",
"score" : 80
}
我可以做类似的事情:
collection.aggregate([
{ $project: {
_id: 1,
"AuthorName": "$author",
"VirtualField": "hardcoded_Value"
}
}
], function (err, doc) {
if (err) return console.error(err);
console.dir(doc);
}
);
谁能告诉如何做同样的事情?
注意:我不想在检索文档后做同样的事情。
出现错误:
[MongoError: exception: field path references must be prefixed with a '$' ('hardcoded_Value ]
name: 'MongoError',
errmsg: 'exception: field path references must be prefixed with a \'$\' (\'hardcoded_Value\'',
code: 15982,
ok: 0 }
使用时出现以下错误:
"VirtualField": {$concat: ["hardcoded_Value"]}
{ [MongoError: exception: invalid operator '$concat']
name: 'MongoError',
errmsg: 'exception: invalid operator \'$concat\'',
code: 15999,
ok: 0 }