what is the mongodb equivalent of the MySQL query
SELECT username AS `consname` FROM `consumer`
An SQL SELECT statement typically retrieves data from tables in a database, much like a mongo shell find statement retrieves documents from a collection in a MongoDB database.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
More Detail. In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB. The MongoDB equivalent syntax is as follows db. yourCollectionName.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With