考慮我想說明以下文件:
{
"_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 }
請確認您的答案中遺漏了某些內容..?我收到錯誤。在我的問題更新.. –
@AmolMKulkarni看起來像concat只能從2.4開始。你的版本是什麼? –
@AmolMKulkarni您需要升級 – Sammaye