[SERVER-3409] WriteResult should indicate number of docs modified (no-ops excluded) Created: 11/Jul/11  Updated: 28/Oct/15  Resolved: 13/Dec/13

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 1.8.2
Fix Version/s: 2.5.5

Type: Improvement Priority: Minor - P4
Reporter: Kyle Banker Assignee: Scott Hernandez (Inactive)
Resolution: Done Votes: 17
Labels: gle, update, updated
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-18639 Expose number of modified documents i... Closed
Duplicate
is duplicated by SERVER-8422 Log/getLastError/profile output repor... Closed
is duplicated by SERVER-2769 Provide a way to find out if $addToSe... Closed
Related
related to SERVER-9038 New write operation method for insert... Closed
related to SERVER-1501 Enhance getlasterror to include indic... Closed
Backwards Compatibility: Fully Compatible
Participants:
Linked BF Score: 0

 Description   

Report both the number of documents ("n") which were matched and have the expected updated values, and the number of docs which were actually changed with a write to the database ("nDocsModified").

Expected:

> db.rb.save({_id:3, a:[1,2]})
Updated 1 existing record(s) in 6ms
Update WriteResult({ "ok" : 1, "nDocsModified" : 1, "n" : 1 ... })
 
// No-op doesn't require modifying the document
> db.rb.save({_id:3, a:[1,2]})
Updated 1 existing record(s) in 2ms
Update WriteResult({ "ok" : 1, "nDocsModified" : 0, "n" : 1 ... })

Note: These changes only apply to the new write commands, not the old wire protocol updates. This means you must be using a new driver/client to see this field in the WriteResults.

Old Description
db.foo.save(

{a: 1, n: [ 'foo' ] }

)
db.foo.update(

{a: 1}

, {$addToSet:

{ n: 'foo' }

} )

db.getPrevError();

{ "err" : null, "updatedExisting" : true, "n" : 1, "nPrev" : 1, "ok" : 1 }

updatedExisting should be false in this case, since the document was not, in fact, updated.



 Comments   
Comment by Githook User [ 13/Dec/13 ]

Author:

{u'username': u'scotthernandez', u'name': u'Scott Hernandez', u'email': u'scotthernandez@gmail.com'}

Message: SERVER-3409: Expose number of docs modified (excl. no-ops)
Branch: master
https://github.com/mongodb/mongo/commit/fe185a30da0616ec9cff3f4fc6a90c025724b64b

Comment by Jeff Seibert [ 23/Nov/13 ]

Just give up and migrate off Mongo like the rest of us. It's been over 2 years. They don't care and they aren't going to fix this issue. Sorry.

Comment by Jeremy Haile [ 23/Nov/13 ]

This is a big problem for me. I currently use redis when adding to sets so that I can know if the element was added or not (and then execute some other behavior)

I'd prefer to migrate this code to mongo, but there's no way to know if an element was added when calling $addToSet.

Comment by KAWAMORITA Mitsuaki [ 20/Aug/13 ]

It's a big problem for me, too
Within one query, I'd like to update (actually, insert) a unique value into embedded and get result whether updated or not.
I know Redis set can solve it, but DRAM is not so cheap.
I hope this problem will be fixed soon.

Comment by Piyush Verma [ 02/Nov/12 ]

+1 Infact there are a bunch of Bugs filed around this behaviour.
https://jira.mongodb.org/browse/SERVER-2769 & https://jira.mongodb.org/browse/SERVER-1501 seem very much the Same.

Comment by Adam Reilly [ 17/Aug/12 ]

This would be a pretty crucial feature for us

Comment by Tucker B [ 09/Aug/12 ]

+1 agree with Tim. I'm having to use Redis for the parts of my application that use set operations because I need to know whether something was actually added to the set or not.

Comment by Tim Oxley [ 13/May/12 ]

+1 this is a really frustrating issue. $addToSet isn't remotely as useful if you can't actually tell if an item was added to the set or not.

Comment by Jeff Seibert [ 08/Dec/11 ]

Any chance of bumping the priority on this? It would make the atomic operators radically more useful.

Here is an example use case: Restaurants and Patrons. Let's say I want to track which restaurants each patron has been to, and how many unique patrons have been to each restaurant, in O(1) time.

Patron:
_id
restaurants = []

Restaurant:
_id
unique_patrons = 0

On each visit to a restaurant:
db.patrons.update({_id: <id>}, {$addToSet: { restaurants: <restaurant_id> }})

IF SUCCESSFUL:
db.restaurants.update({_id: <restaurant_id>}, { $inc:

{ unique_patrons: 1 }

})

Without this functionality, the only safe way to do it is query the entire patrons table for people who have been to that restaurant - recomputing the count every time. Yes, the restaurants field can be indexed, but that is still slower than simply knowing to increment it, and feels like a waste.

Am I correct in that there is currently no other way to do this?

Comment by Scott Hernandez (Inactive) [ 12/Jul/11 ]

It would be great if we could explicitly list the changes. Maybe something like:

{ $addToSet : ###, $push: ###, $set : ### .... }
Comment by Eliot Horowitz (Inactive) [ 12/Jul/11 ]

I don't want to overload updatedExisting.
We can add a new field like "anyChanges"

Generated at Thu Feb 08 03:02:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.