![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Appending an entry in one to many embedded documents with MongoDB
To append an entry in MongoDB, use $push. Let us create a collection with documents −
> db.demo253.insertOne( ... { ... _id: "101", ... isActive: false, ... details: [ ... { ... Name: "Chris", ... }, ... { ... CountryName:"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "101" }
Display all documents from a collection with the help of find() method −
> db.demo253.find();
This will produce the following output −
{ "_id" : "101", "isActive" : false, "details" : [ { "Name" : "Chris" }, { "CountryName" : "US" } ] }
Following is the query to append an entry in one to many embedded documents −
> db.demo253.update({_id: '101'}, {$push: {details: {Age: 21}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo253.find();
This will produce the following output −
{ "_id" : "101", "isActive" : false, "details" : [ { "Name" : "Chris" }, { "CountryName" : "US" }, { "Age" : 21 } ] }
Advertisements