I have some basic users in a collection created by mongoose. My program code created one incorrectly, so I went into the command line to fix it. (its not a big application). I went to use the db.colleciton.findOneAndDelete(...) first, but it deleted the wrong entry.
I was confused at first, but I didn't craft the query in the argument right to match a record, and it should have errored out. When I use the exact same query string in db.collection.findOne(...) it errors and doesn't continue, but with db.collection.findOneAndDelete(...) it does not error, and appears to delete the first record it found.
> db.users
autoradar.users
> db.users.find()
{ "_id" : ObjectId("5cb781472973f80012ea11e5"), "registered" : true, "username" : "hannah", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5d0951717c815e0012af79df"), "registered" : true, "username" : "Harshil ", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5cb4db582973f80012ea11d8"), "registered" : true, "username" : "mac", "password" : "-redacted-", "__v" : 0 }
> db.users.findOne('{ "username" : "mac" }')
2019-06-19T14:14:02.120+0000 E QUERY [js] Error: error: {
"ok" : 0,
"errmsg" : "SyntaxError: missing ; before statement :\nfunctionExpressionParser@src/mongo/scripting/mozjs/mongohelpers.js:48:25\n",
"code" : 139,
"codeName" : "JSInterpreterFailure"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCommandCursor@src/mongo/shell/query.js:708:1
DBQuery.prototype._exec@src/mongo/shell/query.js:113:28
DBQuery.prototype.hasNext@src/mongo/shell/query.js:288:5
DBCollection.prototype.findOne@src/mongo/shell/collection.js:260:10
@(shell):1:1
> db.users.findOneAndDelete('{ "username" : "mac" }')
{
"_id" : ObjectId("5cb781472973f80012ea11e5"),
"registered" : true,
"username" : "hannah",
"password" : "-redacted-",
"__v" : 0
}
> db.users.find()
{ "_id" : ObjectId("5d0951717c815e0012af79df"), "registered" : true, "username" : "Harshil ", "password" : "-redacted-", "__v" : 0 }
{ "_id" : ObjectId("5cb4db582973f80012ea11d8"), "registered" : true, "username" : "mac", "password" : "-redacted-", "__v" : 0 }