How to rename the columnname in mongodb using node js

Hari Smith

I tried to map two collections its working fine but I have doubt in how to rename the column name using mongodb.

promotion collection

{ 
    "_id" : ObjectId("5cf7679a0b0bed2e7483b998"),   
    "group_name" : "Latest",   
    "products" : 
   [ObjectId("5cecc161e8c1e73478956333"),ObjectId("5cecc161e8c1e73478956334")]
}  

product collection

{ 
    "_id" : ObjectId("5cecc161e8c1e73478956333"), 
    "product_name" : "bourbon"
},
{ 
    "_id" : ObjectId("5cecc161e8c1e73478956334"), 
    "product_name" : "bour"
}

mapping query

db.promotional.aggregate(
     [
        {
           $lookup: {
             from: "product",
             localField: "products",
             foreignField: "_id",
             as: "products"
                   }
        },  
        {
           $project :{products :{_id:0}}  
        }

     ]
) 

I got output

{ 
    "_id" : ObjectId("5cf7679a0b0bed2e7483b998"),   
    "group_name" : "Latest",   
    "products" : 
     [
       {  
         "product_name" : "bourbon"
       },
       {  
       "product_name" : "bour"
       }
     ]
}

Expected output

{ 
    "_id" : ObjectId("5cf7679a0b0bed2e7483b998"),   
    "group_name" : "Latest",   
    "products" : 
     [
       {  
         "name" : "bourbon"
       },
       {  
       "name" : "bour"
       }
     ]
}

How to rename productname to name using mongodb

Ashh

You can $map over the products array and can change the fields name

db.promotional.aggregate([
  { "$lookup": {
    "from": "product",
    "localField": "products",
    "foreignField": "_id",
    "as": "products"
  }},
  { "$addFields": {
    "products": {
      "$map": {
        "input": "products",
        "as": "product",
        "in": {
          "name": "$$product.product_name"
        }
      }
    }
  }}
])

If you are using mongodb version 3.6 and above

db.promotional.aggregate([
  { "$lookup": {
    "from": "product",
    "let": { "products": "$products" },
    "pipeline": [
      { "$match": { "$expr": { "$in": [ "$_id", "$$products" ] } } },
      { "$project": { "name": "$product_name", "_id": 0 }}
    ],
    "as": "products"
  }}
])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rename multiple files using Node JS

How to rename files recursively in Node.js?

How to insert array to mongodb using node.js

How to Filter by Debit or Credit in MongoDB using Node.JS

How to delete related schema in mongodb using node js (mongoose)

How to use $inc operator for variables in MongoDB using Node.JS

How to save multiple items in mongodb using node js

How to redirect to view page using mongoDB and node.js

how to retrieve users full data from mongodb using node js

how to implement joi validator using node js and mongodb

How to update data using _id in node_js and mongodb

how to get data from 3 collections in mongodb using node js?

How to display array in frontend using EJS in Mongodb Node js?

how to fetch data from mongodb and display it in a table using node js?

How to map two collections in mongodb using node js

How to fix node.js returning {} when using async with mongoDB

How to delete embedded (nested) document using node.js in mongodb

How to connect to MongoDB when using MVC Node.js?

How to get entry from mongodb using node.js

How to rename a database in mongoDB?

How to rename a user in MongoDB?

How to implement Cursor based pagination for mongodb in node.js using official mongodb client when _id is overrided?

Rename nested field name using $rename in MongoDB

How to download files from server and rename it synchronously in Node.js?

Filtering a query using Node JS and MongoDB

Update single document in mongodb using node js

Node.js: Using Promises with MongoDb

Storing images in MongoDB using Node.js

update the nested array in mongodb using node js