- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
MongoDB query to add multiple documents
To perform multiple write operations, use bulkWrite(). Let us create an array list values. Following is the query −
> const arrayList = [ ... {"Value1":100, "Value2":200, "Name": "John"}, ... {"Value1":100, "Value2":200, "Name": "Bob"} ... ]; > let op1 = []; > arrayList.forEach(({ Value1, Value2, Name }) => { ... op1.push({ ... "updateOne": { ... "filter": { Name}, ... "update": { "$set": { Value1, Value2, Name } }, ... "upsert": true ... } ... }) ... }); > db.demo397.bulkWrite(op1); { "acknowledged" : true, "deletedCount" : 0, "insertedCount" : 0, "matchedCount" : 0, "upsertedCount" : 2, "insertedIds" : { }, "upsertedIds" : { "0" : ObjectId("5e5e8c07f995e1718151981c"), "1" : ObjectId("5e5e8c07f995e1718151981d") } }
Display all documents from a collection with the help of find() method −
> db.demo397.find();
This will produce the following output −
{ "_id" : ObjectId("5e5e8c07f995e1718151981c"), "Name" : "John", "Value1" : 100, "Value2" : 200 } { "_id" : ObjectId("5e5e8c07f995e1718151981d"), "Name" : "Bob", "Value1" : 100, "Value2" : 200 }
- Related Articles
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB query to get documents with multiple conditions set in $or?
- MongoDB query to skip documents
- MongoDB - Query embedded documents?
- MongoDB query to group duplicate documents
- MongoDB query to add up the values of a specific field in documents
- MongoDB filter multiple sub-documents?
- How to update multiple documents in MongoDB?
- How to merge multiple documents in MongoDB?
- MongoDB query to find documents having two values in an array conforming to multiple criteria?
- MongoDB query to skip n first documents?
- Search for multiple documents in MongoDB?
- Fetch specific multiple documents in MongoDB
- How to delete multiple documents in MongoDB using deleteMany()?
- Filter documents in MongoDB using simple query?

Advertisements