- 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
Get number of updated documents in MongoDB?
To get number of updated documents in MongoDB, you need to use runCommand along with getlasterror.
Let us first create a collection with documents
> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca28c1d6304881c5ce84bad") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca28c226304881c5ce84bae") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca28c276304881c5ce84baf") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Ramit"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca28c366304881c5ce84bb0") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca28c436304881c5ce84bb1") }
Following is the query to display all documents from a collection with the help of find() method:
> db.getNumberOfUpdatedDocumentsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca28c1d6304881c5ce84bad"), "StudentName" : "David" } { "_id" : ObjectId("5ca28c226304881c5ce84bae"), "StudentName" : "Chris" } { "_id" : ObjectId("5ca28c276304881c5ce84baf"), "StudentName" : "Robert" } { "_id" : ObjectId("5ca28c366304881c5ce84bb0"), "StudentName" : "Ramit" } { "_id" : ObjectId("5ca28c436304881c5ce84bb1"), "StudentName" : "Adam" }
Following is the query to update documents
> db.getNumberOfUpdatedDocumentsDemo.update({}, {$set : {"StudentName" : "Carol"}}, true, true); WriteResult({ "nMatched" : 5, "nUpserted" : 0, "nModified" : 5 }) Now, get the number of updated documents: > db.runCommand( "getlasterror" );
Following is the output displaying n =5 i.e. 5 documents have been updated
{ "connectionId" : 4, "updatedExisting" : true, "n" : 5, "syncMillis" : 0, "writtenTo" : null, "err" : null, "ok" : 1 }
Now display all documents from a collection
> db.getNumberOfUpdatedDocumentsDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca28c1d6304881c5ce84bad"), "StudentName" : "Carol" } { "_id" : ObjectId("5ca28c226304881c5ce84bae"), "StudentName" : "Carol" } { "_id" : ObjectId("5ca28c276304881c5ce84baf"), "StudentName" : "Carol" } { "_id" : ObjectId("5ca28c366304881c5ce84bb0"), "StudentName" : "Carol" } { "_id" : ObjectId("5ca28c436304881c5ce84bb1"), "StudentName" : "Carol" }
- Related Articles
- Get the count of the number of documents in a MongoDB Collection?
- MongoDB - update partial number of documents?
- Get documents expired before today in MongoDB?
- How to get documents by tags in MongoDB?
- Limit the number of documents in a collection in MongoDB?
- Get the size of all the documents in a MongoDB query?
- Get MongoDB documents that contains specific attributes in array
- Count number of documents from MongoDB collection inside Array?
- MongoDB Group query to get the count of repeated marks in documents?
- Get count of array elements from a specific field in MongoDB documents?
- Get number of records in MongoDB?
- How to count the number of documents in a MongoDB collection?
- Get a count of total documents with MongoDB while using limit?
- MongoDB query to get distinct FirstName values from documents
- Get MongoDB documents with max attribute per group in a collection?

Advertisements