mongodb equivalent of SELECT field AS `anothername` mongodb equivalent of SELECT field AS `anothername` mongodb mongodb

mongodb equivalent of SELECT field AS `anothername`


As it was mentioned by sammaye, you have to use $project in aggregation framework to rename fields.

So in your case it would be:

db.consumer.aggregate([    { "$project": {        "_id": 0,        "consname": "$username"    }}])

Cool thing is that in 2.6.x version aggregate returns a cursor which means it behaves like find.

You might also take a look at $rename operator to permanently change schema.


Salvador Dali's answer is fine, but not working in meteor versions before 3.0.I currently try meteor, and their build in mongodb is in version 2.The way I solved this is like this:

var result = []:db.consumer.forEach( function(doc) {   result.push({     consname:doc.username   });});