- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set a similar name from another column in MongoDB?
Simply loop with forEach() and set column value from another column. Let us create a collection with documents −
> db.demo51.insert({"Name1":"Chris","Name":"David","Age":24}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Carol","Name":"Mike","Age":22}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Sam","Name":"Bob","Age":26}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo51.find();
This will produce the following output −
{ "_id" : ObjectId("5e27108ccfb11e5c34d8990d"), "Name1" : "Chris", "Name" : "David", "Age" : 24 } { "_id" : ObjectId("5e27108dcfb11e5c34d8990e"), "Name1" : "Carol", "Name" : "Mike", "Age" : 22 } { "_id" : ObjectId("5e27108ecfb11e5c34d8990f"), "Name1" : "Sam", "Name" : "Bob", "Age" : 26 }
Following is the query to set a similar name from another column in MongoDB −
> db.demo51.find().forEach( function (d) { ... d.Name1 = d.Name; ... db.demo51.save(d); ... });
Display all documents from a collection with the help of find() method −
> db.demo51.find();
This will produce the following output −
{ "_id" : ObjectId("5e27108ccfb11e5c34d8990d"), "Name1" : "David", "Name" : "David", "Age" : 24 } { "_id" : ObjectId("5e27108dcfb11e5c34d8990e"), "Name1" : "Mike", "Name" : "Mike", "Age" : 22 } { "_id" : ObjectId("5e27108ecfb11e5c34d8990f"), "Name1" : "Bob", "Name" : "Bob", "Age" : 26 }
- Related Articles
- Renaming column name in a MongoDB collection?
- MongoDB: Find name similar to Regular Expression input?
- MySQL query to group by column and display the sum of similar values in another column
- Merging 2 tables with similar column name SAP HANA database
- Get distinct values from a column in MongoDB?
- Group concatenate the last name from a MySQL column and set a condition to display limited records
- How to add column from another DataFrame in Pandas?
- How to copy a collection from one database to another in MongoDB?
- Searching a specific domain name from URL records in MongoDB?
- Can we add a column to a table from another table in MySQL?
- Group by one column and display corresponding records from another column with a separator in MySQL
- Setting similar value for a column in a MySQL table?
- How to get column index from column name in Python Pandas?
- MySQL query to calculate sum from 5 tables with a similar column named “UP”?
- Implement a query similar to MySQL Union with MongoDB?

Advertisements