- 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
I want to create a new field in an already created document. How can this be done using MongoDB query?
Use $addToSet to create a new field in MongoDB. Let us first create a collection with documents −
> db.createFieldDemo.insertOne({"StudentFirstName":"John","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e28b50a6c6dd317ad95") } > db.createFieldDemo.insertOne({"StudentFirstName":"Larry","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e2fb50a6c6dd317ad96") } > db.createFieldDemo.insertOne({"StudentFirstName":"Chris","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e38b50a6c6dd317ad97") } > db.createFieldDemo.insertOne({"StudentFirstName":"David","StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99e43b50a6c6dd317ad98") }
Following is the query to display all documents from a collection with the help of find() method −
> db.createFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd99e28b50a6c6dd317ad95"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd99e2fb50a6c6dd317ad96"), "StudentFirstName" : "Larry", "StudentAge" : 23 } { "_id" : ObjectId("5cd99e38b50a6c6dd317ad97"), "StudentFirstName" : "Chris", "StudentAge" : 22 } { "_id" : ObjectId("5cd99e43b50a6c6dd317ad98"), "StudentFirstName" : "David", "StudentAge" : 25 }
Following is the query to create a new field. Here, we are creating a field “StudentLastName” −
> db.createFieldDemo.update({_id: ObjectId("5cd99e43b50a6c6dd317ad98")}, {$addToSet: {"StudentLastName": "Miller"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check all documents from the above collection −
> db.createFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd99e28b50a6c6dd317ad95"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd99e2fb50a6c6dd317ad96"), "StudentFirstName" : "Larry", "StudentAge" : 23 } { "_id" : ObjectId("5cd99e38b50a6c6dd317ad97"), "StudentFirstName" : "Chris", "StudentAge" : 22 } { "_id" : ObjectId("5cd99e43b50a6c6dd317ad98"), "StudentFirstName" : "David", "StudentAge" : 25, "StudentLastName" : [ "Miller" ] }
- Related Articles
- MongoDB query to add a document in an already created collection
- How can a new column be created to a dataframe using the already present columns in Python?
- How to create and fill a new column in an already created MySQL table?
- Add new field to every document in a MongoDB collection?
- MongoDB query to add new array element in document
- MongoDB query to create new field and count set the count of another field in it?
- How to add a column using MySQL SELECT in an already created table?
- Want to update inner field in a MongoDB
- How to add an extra field in a sub document in MongoDB?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB - how can I access fields in a document?
- How to randomize an already created vector in R?
- Add a field to an embedded document in an array in MongoDB?
- How to query a document in MongoDB comparing fields from an array?
- MongoDB query to fetch a document that does not have a particular field?

Advertisements