javascript - In NodeJS, how to output results from mongodb with different field names? -


i'm using nodejs query mongodb , want output json customized field names.

for example, original json mongodb maybe

    {id:1, text:"abc"} 

i'd output

    {objectid:1, displaytext:"abc"}; 

i understand mongodb has $project operator in aggregate framework not sure how use them in nodejs.

the mongodb nodejs packages i'm using

    var mongo = require('mongodb');     var monk = require('monk');     var db = monk('server:port/mydb'); 

appreciate advice on this.

if using monk appear can access underlying node native driver collection type via .col accessor on selected collection object:

  var db = require('monk')('localhost/test')     , collection = db.get('example');    collection.col.aggregate(     [       { "$project": {         "_id": 0,         "objectid": "$_id",         "displaytext": "$text"       }}     ],     function(err,result) {        console.log( json.stringify( result, undefined, 4 ) );      }   ); 

note methods such .aggregate() retrieved in way not wrapped in promise object standard monk collection objects are. @ least shows how access , use $project re-shape document.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -