
- 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 set a sub item in an array?
You can use positional $ operator. Let us first create a collection with documents −
> db.demo22.insertOne( ... { ... ProductId:101, ... ... ProductDetails: ... [ ... { ... ProductFirstPrice: '35', ... ProductSecondPrice: '75' ... }, ... { ... ProductFirstPrice: '', ... ProductSecondPrice:'' ... }, ... { ... ProductFirstPrice: '78', ... ProductSecondPrice:'24' ... } ... ] ... } ... ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e14c0b422d07d3b95082e70") }
Display all documents from a collection with the help of find() method −
> db.demo22.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e14c0b422d07d3b95082e70"), "ProductId" : 101, "ProductDetails" : [ { "ProductFirstPrice" : "35", "ProductSecondPrice" : "75" }, { "ProductFirstPrice" : "", "ProductSecondPrice" : "" }, { "ProductFirstPrice" : "78", "ProductSecondPrice" : "24" } ] }
Following is the MongoDB query to set a sub item in an array −
> db.demo22.update({ "ProductDetails.ProductFirstPrice" : "35" }, ... { $set : { "ProductDetails.$.ProductFirstPrice" : "" }}, false, true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo22.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e14c0b422d07d3b95082e70"), "ProductId" : 101, "ProductDetails" : [ { "ProductFirstPrice" : "", "ProductSecondPrice" : "75" }, { "ProductFirstPrice" : "", "ProductSecondPrice" : "" }, { "ProductFirstPrice" : "78", "ProductSecondPrice" : "24" } ] }
- Related Articles
- MongoDB query for capped sub-collection in an array
- MongoDB query to remove item from array?
- MongoDB query to gather unique array item?
- MongoDB query to remove element from array as sub property
- Pull an element in sub of sub-array in MongoDB?
- MongoDB query by sub-field?
- How to add a sub-document to sub-document array in MongoDB?
- MongoDB query to access an object in an array
- MongoDB query to replace value in an array?
- Query an array in MongoDB to fetch a specific value
- Is there a MongoDB query to concatenate deep sub-lists?
- How to fire find query on sub-documents in MongoDB?
- How do you limit an array sub-element in MongoDB?
- How to insert an item to an array that is inside an object in MongoDB?
- MongoDB query to push document into an array

Advertisements