
- 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
Implement MongoDB $addToSet for an array in an array and append a value
For this, use update() along with $addToSet. The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array. Let us create a collection with documents −
> db.demo509.insertOne( ... { ... ... "value1" : [ ... { ... "value2" : [ ... 76, ... 14, ... 56 ... ] ... }, ... { ... ... "value2" : [ ... 12, ... 19, ... 91 ... ] ... }, ... { ... ... "value2" : [ ... 87 ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e88421d987b6e0e9d18f57d") }
Display all documents from a collection with the help of find() method −
> db.demo509.find();
This will produce the following output −
{ "_id" : ObjectId("5e88421d987b6e0e9d18f57d"), "value1" : [ { "value2" : [ 76, 14, 56 ] }, { "value2" : [ 12, 19, 91 ] }, { "value2" : [ 87 ] } ] }
Following is the query to implement $addToSet and append in an array −
> db.demo509.update({},{$addToSet:{"value1.2.value2":1000}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo509.find();
This will produce the following output −
{ "_id" : ObjectId("5e88421d987b6e0e9d18f57d"), "value1" : [ { "value2" : [ 76, 14, 56 ] }, { "value2" : [ 12, 19, 91 ] }, { "value2" : [ 87, 1000 ] } ] }
- Related Articles
- Replace an array field value with MongoDB?
- MongoDB query to replace value in an array?
- Increment value of an array element with array object in MongoDB
- MongoDB $addToSet to add a deep nested array of object?
- Query an array in MongoDB to fetch a specific value
- Check for duplicates in an array in MongoDB?
- MongoDB query for Partial Object in an array
- Golang Program To Append An Element Into An Array
- Swift Program to append an element into an array
- C++ Program to append an element into an array
- How to append to array in MongoDB?
- Implement array match in MongoDB?
- How to append an item into a JavaScript array?
- MongoDB syntax for updating an object inside an array within a document?
- MongoDB query for capped sub-collection in an array

Advertisements