Aravindh
Aravindh

Reputation: 451

how to get alias name on fetching the data from mongoDB using find query

I am using MongoDB, my table(table name "My_Table") looks like

attributes: {
        user_id: {
            type: 'string'
        }}

I like to get field name user_id as user as alias name

I able to get the value for My_Table using find query My_Table.find({}).then(function(data){console.log(data)}) using this query i can get the output as {user_id: "11111"} but my expected output with alias name as {user : "11111"}

for this I don't know whether I need to get add some field in attributes or need to change my find method.

anyone please help to get through this issue, thanks in advance

Upvotes: 1

Views: 795

Answers (1)

Shubham
Shubham

Reputation: 1426

My_Table.aggregate([
{$match:{}},
{$project:{user:"$user_id",id:1}}
])

In $project or $addFields stage you can format your output whatever you want

Upvotes: 1

Related Questions