
- 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
MongoDB query to add new array element in document
To add new array element in a MongoDB document, use $(projection) along with update(). Let us create a collection with documents −
>db.demo222.insertOne({"details":[{"StudentName":"Chris","StudentMarks":78},{"StudentName":"David","StudentMarks":89}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee31703d395bdc213472f") }
Display all documents from a collection with the help of find() method −
> db.demo222.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e3ee31703d395bdc213472f"), "details" : [ { "StudentName" : "Chris", "StudentMarks" : 78 }, { "StudentName" : "David", "StudentMarks" : 89 } ] }
Following is the query to add new array element in document −
> db.demo222.update({"details.StudentName" : "Chris"},{"$set" : {"details.$.SubjectName":"MongoDB"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo222.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e3ee31703d395bdc213472f"), "details" : [ { "StudentName" : "Chris", "StudentMarks" : 78, "SubjectName" : "MongoDB" }, { "StudentName" : "David", "StudentMarks" : 89 } ] }
- Related Articles
- Add new field to every document in a MongoDB collection?
- How to add a sub-document to sub-document array in MongoDB?
- MongoDB query to push document into an array
- MongoDB query to add a document in an already created collection
- Include all existing fields and add new fields to document in MongoDB?
- MongoDB query to remove array elements from a document?
- How to add new item in nested array with MongoDB?
- Filter query on array of embedded document with MongoDB?
- 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 update nested document
- How to pull an array element (which is a document) in MongoDB?
- MongoDB query to get average in aggregation of array element?
- MongoDB query to pull array element from a collection?
- MongoDB query to slice only one element of array

Advertisements