How to create something like virtual field & hardcode a value in mongodb aggregation $project? -
consider want show following document:
{ "_id" : objectid("512bc95fe835e68f199c8686"), "authorname": "dave", "virtualfield" : "hardcoded_value" }
actual document stored in mongodb
{ "_id" : objectid("512bc95fe835e68f199c8686"), "author": "dave", "score" : 80 }
can :
collection.aggregate([ { $project: { _id: 1, "authorname": "$author", "virtualfield": "hardcoded_value" } } ], function (err, doc) { if (err) return console.error(err); console.dir(doc); } );
could tell how same?
note: don't want same after retrieving document.
got error :
[mongoerror: exception: field path references must prefixed '$' ('hardcoded_value ] name: 'mongoerror', errmsg: 'exception: field path references must prefixed \'$\' (\'hardcoded_value\'', code: 15982, ok: 0 }
getting following error on using:
"virtualfield": {$concat: ["hardcoded_value"]}
{ [mongoerror: exception: invalid operator '$concat'] name: 'mongoerror', errmsg: 'exception: invalid operator \'$concat\'', code: 15999, ok: 0 }
you should use concat
here: http://docs.mongodb.org/manual/reference/aggregation/#exp._s_concat
like so:
"virtualfield": {$concat: ["hardcoded_value"]}
Comments
Post a Comment