- 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
How to add a column in MongoDB collection?
To add a column, you need to update the collection. The syntax is as follows −
db.getCollection(yourCollectionName).update({}, {$set: {"yourColumnName": "yourValue"}},false,true);
To understand the above syntax, let us create a collection with documents −
> db.addColumnDemo.insertOne({"StudentId":101,"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e04d66af5e889d7a519950f") } > db.addColumnDemo.insertOne({"StudentId":102,"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e04d673f5e889d7a5199510") } > db.addColumnDemo.insertOne({"StudentId":103,"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e04d67bf5e889d7a5199511") }
Following is the query to display all documents from a collection with the help of find() method −
> db.addColumnDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e04d66af5e889d7a519950f"), "StudentId" : 101, "StudentName" : "Chris" } { "_id" : ObjectId("5e04d673f5e889d7a5199510"), "StudentId" : 102, "StudentName" : "Robert" } { "_id" : ObjectId("5e04d67bf5e889d7a5199511"), "StudentId" : 103, "StudentName" : "David" }
Here is the query to add a column in MongoDB −
> db.getCollection('addColumnDemo').update({}, {$set: {"StudentCityName": "New York"}},false,true); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Following is the query to display all documents from a collection with the help of find() method −
> db.addColumnDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e04d66af5e889d7a519950f"), "StudentId" : 101, "StudentName" : "Chris", "StudentCityName" : "New York" } { "_id" : ObjectId("5e04d673f5e889d7a5199510"), "StudentId" : 102, "StudentName" : "Robert", "StudentCityName" : "New York" } { "_id" : ObjectId("5e04d67bf5e889d7a5199511"), "StudentId" : 103, "StudentName" : "David", "StudentCityName" : "New York" }
- Related Articles
- Renaming column name in a MongoDB collection?
- Add new field to every document in a MongoDB collection?
- How to add a new field to all the documents in a MongoDB collection
- Add MD5 hash value to MongoDB collection?
- Script to add a single value to array in MongoDB collection?
- MongoDB query to add a document in an already created collection
- How to drop a collection in MongoDB?
- Add field that is unique index to collection in MongoDB?
- How to create a new collection in MongoDB?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- How to check empty field in a MongoDB collection?
- How to add a new value to a collection in Laravel?
- How to get array from a MongoDB collection?
- How to drop a numeric collection from MongoDB?

Advertisements