[SERVER-35107] BulkWriteResult has NOT updatedCount property Created: 21/May/18  Updated: 27/Oct/23  Resolved: 18/Jun/18

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: bo yuan Assignee: Nick Brewer
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:
  1. create a collection with sample docs inserted
  2. use bulkWrite(ops) to execute a list of UpdatOne commands
  3. check the returned result object
Participants:

 Description   

with a list of UpdatOne commands executed by bulkWrite(ops), the BulkWriteResult object returned has only property like matchedCount, upsertedCount, etc, with NO updatedCount or modifiedCount. How do I know the number of docs actually updated?



 Comments   
Comment by Nick Brewer [ 18/Jun/18 ]

Hi,

You can determine the number of updated documents using bulkWriteResult via the nModified property. Since your script is not setting upserting to true or performing any inserts, you're only modifying existing documents - this means that the value of nModified should be sufficient to tell you how many documents have been updated. If you were to introduce upserts or inserts, you would need to make use of additional properties available to bulkWriteResult to see the total amount of documents created and modified. See the documentation for a full list of properties.

I do not see anything to indicate a bug in the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag.

Nick

Comment by bo yuan [ 22/May/18 ]

dear Mr Fernandez,

My mongo is 3.6.0.

Although updatedCount is not found in the bulkwrite result object, the documents in mongo collections have been actually update. 

my script is like this:

var cursor = db.news_hexun.find({'news_item': {'$exists': true }}, {body:0})
var requests = [];
cc = 0;
cursor.forEach(doc => {
 requests.push({
 'updateOne':{
 'filter': {'_id': doc._id},
 'update': {'$set': {'item_url': doc.news_item.url}, '$unset': {"news_item":""} }
 }
 });
 
 if (requests.length >= 10000 ){
 var results = db.news_hexun.bulkWrite(requests);
 print('matchedCount: ' + results.matchedCount)
 //print('updatedCount: ' + results.modifiedCount)
 cc += requests.length ;
 print(cc)
 requests = []
 }
});
 
if(requests.length > 0){
 var results = db.news_hexun.bulkWrite(requests);
 print('matchedCount: ' + results.matchedCount)
 //print('updatedCount: ' + results.modifiedCount)
 cc += requests.length ;
 print(cc)
 requests = []
}

Comment by Ramon Fernandez Marina [ 21/May/18 ]

rambo_yuanbo@outlook.com, do you happen to have a script to reproduce this issue? Also, on which version of MongoDB are you seeing this behavior? Does it reproduce in 3.6.5?

Thanks,
Ramón.

Generated at Thu Feb 08 04:38:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.