
- 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
Script to add a single value to array in MongoDB collection?
To add only a single value to array, use $push. Let us first create a collection with documents −
> db.demo3.insertOne( ... { ... "Information" : { ... "Of" : { ... "StudentCode" : "STUDENT101", ... "StudentFavouriteSubject" : [ ... "MySQL", ... "Java" ... ] ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e08b7ef25ddae1f53b6221b") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo3.find();
This will produce the following output −
{ "_id" : ObjectId("5e08b7ef25ddae1f53b6221b"), "Information" : { "Of" : { "StudentCode" : "STUDENT101", "StudentFavouriteSubject" : [ "MySQL", "Java" ] } } }
Here is the query to add only a single value to array in MongoDB −
> db.demo3.update({},{$push:{"Information.Of.StudentFavouriteSubject":"Spring and Hibernate"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check all the documents once again −
> db.demo3.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e08b7ef25ddae1f53b6221b"), "Information" : { "Of" : { "StudentCode" : "STUDENT101", "StudentFavouriteSubject" : [ "MySQL", "Java", "Spring and Hibernate" ] } } }
- Related Articles
- Add MD5 hash value to MongoDB collection?
- How to add a column in MongoDB collection?
- Sort MongoDB Collection by Array value?
- How to update a single field in a capped collection in MongoDB?
- Add new field to every document in a MongoDB collection?
- How to pop a single value in MongoDB?
- How to add a new value to a collection in Laravel?
- How to get array from a MongoDB collection?
- MongoDB query to add a document in an already created collection
- How to add a new field to all the documents in a MongoDB collection
- Add field that is unique index to collection in MongoDB?
- Make MongoDB replace single array value with string?
- How to remove all documents from a collection except a single document in MongoDB?
- MongoDB query to pull array element from a collection?
- How do I add a value to the top of an array in MongoDB?

Advertisements