
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to update a MongoDB document for adding a new item to an array?
To add a new item to an array, you can use $push operator. Let us first implement the following query to create a collection with documents:
> db.updateDemo.insertOne({"StudentName":"Larry","StudentCoreSubject":["Java","C"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c98ba78330fd0aa0d2fe4c9") } >db.updateDemo.insertOne({"StudentName":"Robert","StudentCoreSubject":["C++","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c98ba8b330fd0aa0d2fe4ca") } > db.updateDemo.insertOne({"StudentName":"Chris","StudentCoreSubject":["Python"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c98ba9b330fd0aa0d2fe4cb") }
Following is the query to display all the documents from a collection with the help of find() method
> db.updateDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c98ba78330fd0aa0d2fe4c9"), "StudentName" : "Larry", "StudentCoreSubject" : [ "Java", "C" ] } { "_id" : ObjectId("5c98ba8b330fd0aa0d2fe4ca"), "StudentName" : "Robert", "StudentCoreSubject" : [ "C++", "MongoDB" ] } { "_id" : ObjectId("5c98ba9b330fd0aa0d2fe4cb"), "StudentName" : "Chris", "StudentCoreSubject" : [ "Python" ] }
Following is the query to add a new item to an array
> db.updateDemo.update( { _id:ObjectId("5c98ba78330fd0aa0d2fe4c9") }, { $push: { "StudentCoreSubject": "MySQL" } }); Updated 1 existing record(s) in 2ms WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Now check the item is inserted into the array or not
> db.updateDemo.find().pretty();
This will produce the following output. The string “MySQL” inserted successfully in the id 5c98ba78330fd0aa0d2fe4c9
{ "_id" : ObjectId("5c98ba78330fd0aa0d2fe4c9"), "StudentName" : "Larry", "StudentCoreSubject" : [ "Java", "C", "MySQL" ] } { "_id" : ObjectId("5c98ba8b330fd0aa0d2fe4ca"), "StudentName" : "Robert", "StudentCoreSubject" : [ "C++", "MongoDB" ] } { "_id" : ObjectId("5c98ba9b330fd0aa0d2fe4cb"), "StudentName" : "Chris", "StudentCoreSubject" : [ "Python" ] }
- Related Articles
- Update a single list item of a MongoDB document?
- Adding new property to each document in a large MongoDB collection?
- Updating a MongoDB document and adding new keys only in the first document?
- MongoDB findOneAndUpdate() to update a single document
- How to update the _id of a MongoDB Document?
- How to add new item in nested array with MongoDB?
- MongoDB query to add new array element in document
- How to update an existing document in MongoDB collection using Java?
- MongoDB query to update a specific document from a collection
- How to update a MongoDB document without overwriting the existing one?
- How to add a sub-document to sub-document array in MongoDB?
- MongoDB query to update nested document
- MongoDB query to set a sub item in an array?
- How to update document with marks value in MongoDB for student David
- How to query a document in MongoDB comparing fields from an array?

Advertisements