[SERVER-22096] Detect writeConcernError from findAndModify() shell helper Created: 07/Jan/16  Updated: 06/Dec/22  Resolved: 03/Dec/21

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

Type: Improvement Priority: Major - P3
Reporter: Kyle Suarez Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to DOCS-6898 findAndModify takes a write concern Closed
Assigned Teams:
Server Tooling & Methods
Participants:

 Description   

When using the shell helper db.collection.findAndModify(), you cannot determine if a write concern error has occurred. You can only do so via the dbcommand version of findAndModify:

replset:PRIMARY> db.products.find()
{ "_id" : ObjectId("568eb38f3a04b65e35ff642c"), "sku" : 1, "field" : 9 }
replset:PRIMARY> db.runCommand( { findAndModify: "products", query: { sku: 1 }, update: { $inc: { field: 1 } }, writeConcern: { w: 3, wtimeout: 5000 } } )
{
        "lastErrorObject" : {
                "updatedExisting" : true,
                "n" : 1
        },
        "value" : {
                "_id" : ObjectId("568eb38f3a04b65e35ff642c"),
                "sku" : 1,
                "field" : 9
        },
        "writeConcernError" : {
                "code" : 64,
                "errmsg" : "waiting for replication timed out"
        },
        "ok" : 1
}
replset:PRIMARY> db.products.findAndModify( { query: { sku: 1 }, update: { $inc: { field: 1 } }, writeConcern: { w: 3, wtimeout: 5000 } } )
{ "_id" : ObjectId("568eb38f3a04b65e35ff642c"), "sku" : 1, "field" : 10 }



 Comments   
Comment by Brooke Miller [ 03/Dec/21 ]

The shell is deprecated to external users, so please feel free to re-open if you are a core-db contributor and would like this feature for correctness testing purposes.

Comment by Kyle Suarez [ 07/Jan/16 ]

It's interesting to note that findAndModify returns {ok: 1} even when nothing has been written and a writeConcern error exists:

replset:PRIMARY> db.runCommand( { findAndModify: "nonexistent", query: {sku: 1}, update: { $inc: {field: 1} }, writeConcern: { w: 7, wtimeout: 5000 }})
{
        "lastErrorObject" : {
                "updatedExisting" : false,
                "n" : 0
        },
        "value" : null,
        "writeConcernError" : {
                "code" : 100,
                "errmsg" : "Not enough data-bearing nodes"
        },
        "ok" : 1
}

Comment by J Rassi [ 07/Jan/16 ]

I suspect I'm a couple of years late to the game here, but I'm surprised to learn that the write commands and findAndModify return {ok: 1} even if the write concern cannot be satisfied. "createUser" returns {ok: 0} in this case (and I suspect the other auth commands, too).

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