
- 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 insert an array element with a condition?
Let us first create a collection with documents −
>db.demo11.insertOne({"ListOfStudent":[{"StudentName":"Chris","ListOfScore":[76,67,54,89]}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f6e34d7df943a7cec4fa1") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo11.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"), "ListOfStudent" : [ { "StudentName" : "Chris", "ListOfScore" : [ 76, 67, 54, 89 ] } ] }
Here is the query to insert an array element with a condition −
> db.demo11.update( {"ListOfStudent.StudentName":"Chris"}, {$push:{"ListOfStudent.$.ListOfScore":98}} ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the documents once again −
> db.demo11.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"), "ListOfStudent" : [ { "StudentName" : "Chris", "ListOfScore" : [ 76, 67, 54, 89, 98 ] } ] }
- Related Articles
- MongoDB query to update an array element matching a condition using $push?
- MongoDB query to match each element in a documents array to a condition?
- Update an array element matching a condition using $push in MongoDB
- MongoDB query to get record beginning with specific element from an array?
- MongoDB query with an 'or' condition?\n
- MongoDB query to match and remove element from an array?
- MongoDB query to pull array element from a collection?
- Query a nested field within an array with MongoDB
- Query nested array by more than one condition in MongoDB
- MongoDB query where all array items are less than a specified condition?
- MongoDB query where all array items are greater than a specified condition?
- MongoDB query condition to compare two fields?
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to add new array element in document
- MongoDB query to slice only one element of array

Advertisements