44

I think that findOneAndUpdate carries out an atomic operation, so I'm assuming that updateOne does not.

Why would you choose updateOne over findOneAndUpdate and avoid an atomic operation and have to spend extra time checking if the updates were atomic?

I would appreciate some insight or a use case.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Nick Pineda
  • 6,354
  • 11
  • 46
  • 66
  • 1
    "findOneAndUpdate" seems to suggest, "find a document and then update it" and the description makes it sounds like the key point of it is to do those 2 things in one go and atomically. The idea that it is an extension of "find" and not "update", in regards to what it is being return, might be apparent to someone who has been through the naming evolution of the API, but not so much to new people coming in. I think highlighting the different return types in the doc would really help clear that up in a newcomers mind. – Nick Pineda Mar 24 '16 at 22:11
  • @BlakesSeven all that I said above applies to the Node Driver Docs. – Nick Pineda Mar 24 '16 at 22:17
  • @JohnnyHK this question is updateOne/findOneAndUpdate. The duplicate you tagged is findAndModify/update. https://stackoverflow.com/questions/31808786/ would be a better duplicate to link. – ZachB Aug 29 '18 at 19:15
  • @ZachB updateOne/findOneAndUpdate are just wrappers around update/findAndModify, so the dupe still applies. – JohnnyHK Aug 29 '18 at 19:21
  • @JohnnyHK yeah, but not everyone knows that and neither of those methods are mentioned at all in the linked question. 31808786 would be more helpful because of that, and it has a somewhat more concise/explicit answer I think. – ZachB Aug 29 '18 at 19:26
  • Eh maybe the linked one is better. /shrug. Glad to have the comment here that says they're synonyms though. – ZachB Aug 29 '18 at 19:37
  • 1
    @ZachB I've added the one you listed as an alternative dupe. Might help someone. – JohnnyHK Aug 29 '18 at 19:38
  • If a sizeable explanation is needed to understand how the dupe applies (something turns out to be a wrapper over something mentioned there etc), this question warrants a separate answer that shows the connection. Voting to reopen as such. – ivan_pozdeev Aug 29 '18 at 21:30

1 Answers1

73

I think that findOneAndUpdate carries out an atomic operation, so I'm assuming that updateOne does not.

Why are you assuming that?

findOneAndUpdate returns a document whereas updateOne does not (it just returns the _id if it has created a new document).

I think that's the main difference. So the use case of updateOne is when you don't need the document and want to save a bit of time and bandwidth.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Ilya
  • 5,377
  • 2
  • 18
  • 33
  • 3
    No it does not return the "modified" `_id`. The only thing consistently returned is a `WriteResult`, which has indicators of how many matched and how many updated. Only with the `"upsert"` option would the "inserted id" be returned. – Blakes Seven Mar 24 '16 at 21:22
  • 2
    @BlakesSeven right, it doesn't *always* retuns the id, but the point is that it doesn't return the document (and so sends less data). NB I've added that precision to the answer. – Ilya Mar 24 '16 at 21:30
  • 4
    @Ilya I assumed the atomic part to be the key difference because that's all the description gave me. Also they both describe the return value as just "promise" which, in my opinion, makes building an intuition from reading the docs really hard. – Nick Pineda Mar 24 '16 at 21:56
  • @NickPineda you should use the MongoDB doc (don't know which one you read) https://docs.mongodb.org/manual/reference/method/db.collection.updateOne/ – Ilya Mar 24 '16 at 22:07
  • @Ilya I've been going off the Node Driver docs - http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#updateOne - It always think of those docs as applicable to the mongo shell and have run into a few differences here and there with the driver that caused me to just stick with the driver docs. – Nick Pineda Mar 24 '16 at 22:16
  • @NickPineda indeed the driver doc doesn't describe the returned infos. – Ilya Mar 24 '16 at 22:36
  • @BlakesSeven Didn't mean to come off as arguing. I just thought I'd share my perspective from this side of the familiarity spectrum. Every coding language and API is like picking up a different tongue and a dialect and new people can tripped up with all sorts of ambiguity issues that seem silly to someone who is highly familiar/proficient with it. I don't think many Docs(especially open source) are written with a newbie audience in mind, but I can't complain because it's free and awesome! Thanks a lot for the help! – Nick Pineda Mar 24 '16 at 23:59
  • 13
    @NickPineda IMO you're right in arguing that the naming is not very good (and that's **not** a "english-as-a-second-language" problem). `findOneAndUpdate` does not imply a returned document, should be `updateAndFetch` or something. And yes the driver doc is lacking. – Ilya Mar 25 '16 at 08:27