
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to copy attributes in MongoDB?
To copy the value of one attribute to another, use $set along with update(). Let us create a collection with documents −
> db.demo55.insertOne({"ShippingDate":'',"date":new ISODate("2019-01-21")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2716dfcfb11e5c34d89915") } > db.demo55.insertOne({"ShippingDate":'',"date":new ISODate("2020-05-12")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2716ebcfb11e5c34d89916") }
Display all documents from a collection with the help of find() method −
> db.demo55.find();
This will produce the following output −
{ "_id" : ObjectId("5e2716dfcfb11e5c34d89915"), "ShippingDate" : "", "date" : ISODate("2019-01-21T00:00:00Z") } { "_id" : ObjectId("5e2716ebcfb11e5c34d89916"), "ShippingDate" : "", "date" : ISODate("2020-05-12T00:00:00Z") }
Following is the query to copy attributes in MongoDB −
> db.demo55.find({}).forEach(function(c){ ... db.demo55.update({_id: c._id}, {$set: {ShippingDate:c.date}}); ... });
Display all documents from a collection with the help of find() method −
> db.demo55.find();
This will produce the following output −
{ "_id" : ObjectId("5e2716dfcfb11e5c34d89915"), "ShippingDate" : ISODate("2019-01-21T00:00:00Z"), "date" : ISODate("2019-01-21T00:00:00Z") } { "_id" : ObjectId("5e2716ebcfb11e5c34d89916"), "ShippingDate" : ISODate("2020-05-12T00:00:00Z"), "date" : ISODate("2020-05-12T00:00:00Z") }
- Related Questions & Answers
- How to compare attributes of different objects in MongoDB object array?
- MongoDB - How to copy rows into a newly created collection?
- How to copy a collection from one database to another in MongoDB?
- Get MongoDB documents that contains specific attributes in array
- How to construct custom attributes in C#?
- How to work with attributes in C#
- How to create custom attributes in C#?
- How to copy multiple files in PowerShell using Copy-Item?
- How to define multiple CSS attributes in jQuery?
- attributes in C++
- MongoDB query to convert an array to a map of documents with n attributes?
- HTML Attributes
- How to define attributes of a class in Python?
- How to use height and width attributes in HTML?
- How to use min and max attributes in HTML?
Advertisements