[SERVER-42741] getting back both _id of modified documents and writeResult information Created: 09/Aug/19  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: Shell, Usability
Affects Version/s: 4.0.12, 4.2.0
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Ben Rotz Assignee: Backlog - Query Execution
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-714 Allow findandmodify to retreive more ... Open
related to SERVER-28145 add ability to obtain modified flag f... Backlog
Assigned Teams:
Query Execution
Participants:
Case:

 Description   

For many years I've fought with mongodb when I want to perform an upsert operation that both:
 
1) returns nModified/nUpserted/etc. result (e.g. https://docs.mongodb.com/manual/reference/method/WriteResult/)
2) returns the id(s) of any modified records. (unfortunately, only an upsert returns WriteResult._id). Returning the fully modified document is even better, but usually my use cases would be sufficient with returning the _id
 
For example,
 
 

db.test.insert({fruit: 'apple'});
db.test.insert({fruit: 'banana'});
db.test.insert({fruit: 'pear'});

 
 

db.test.update({fruit: 'orange'}, {$set: {fruit: 'grape'}}, {upsert: true});

will give me nUpserted 1, and _id because the operation resulted in an upsert.
 

db.test.update({fruit: 'apple'}, {$set: {fruit: 'grape'}}, {upsert: true});

will give me nModified 1, but no _id because the operation was not an upsert.
 
db.test.findAndModify() returns the record (either old or new as defined by the option), but no WriteResult information so it is not possible to determine if nModified happened or not.
 
I increasingly find reasons when writing code to want to get this type of information back in one operation.
 
 
 



 Comments   
Comment by Ben Rotz [ 12/Sep/19 ]

I’m not sure what the solution to 714 would look like so I’m not sure.

However, your other linked issue https://jira.mongodb.org/browse/SERVER-28145 seems like what ok actually looking for. For my specific use case, ideally I could run a find and modify and get back both the old and the new document, as well as writeresult (nmodified, nmatched, etc.). I realize that getting back both old and new may introduce a lot of complexity to the internals that I don’t appreciate so I am content with merely getting back nmodified as stated in 28145.

Comment by Asya Kamsky [ 12/Sep/19 ]

Looks like that improvement request already exists as SERVER-28145 which I linked to this ticket as well as SERVER-714

Comment by Asya Kamsky [ 12/Sep/19 ]

> [findAndModify returns] no WriteResult

Note that while the shell helper just returns the document (old or new) the findAndModify command returns result in this format:

{
	"lastErrorObject" : {
		"n" : 1,
		"updatedExisting" : true
	},
	"value" : {
		"_id" : ObjectId("5a29ef13bc6fbcd8b34230c1")
	},
	"ok" : 1
}
// or 
{
	"lastErrorObject" : {
		"n" : 1,
		"updatedExisting" : false,
		"upserted" : ObjectId("5d7a5cbc892cfe4f0ea19b23")
	},
	"value" : null,
	"ok" : 1
}
 

Unfortunately it seems to not have a way to return information differentiating existing found and updated and existing found and update was a no-op. I'll look into this and file a SERVER ticket if appropriate.

Comment by Asya Kamsky [ 12/Sep/19 ]

ben@ethika.com it seems like this would be possible if SERVER-714 was implemented, would you agree?

 

Comment by Danny Hatcher (Inactive) [ 15/Aug/19 ]

Thanks for the report and the examples. I'll forward this along to the appropriate team to decide possible next steps.

Generated at Thu Feb 08 05:01:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.