[DOCS-861] Clarify behavior of findAndModify with upsert Created: 07/Dec/12  Updated: 13/Nov/23  Resolved: 19/Feb/13

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: mongodb-2.2, Server_Docs_20231030, Server_Docs_20231106, Server_Docs_20231105, Server_Docs_20231113

Type: Bug Priority: Major - P3
Reporter: Andy Schwerin Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-7170 Upserts using unidexed query can resu... Closed
is related to SERVER-20158 Errors during findAndModify runs on M... Closed
is related to DOCS-1429 Clarify ramification of upsert in upd... Closed
Participants:
Days since reply: 11 years, 1 day ago

 Description   

If several clients dispatch a findAndModify command on the same target collection, under certain circumstances, multiple documents may be inserted. The simplest case to understand is when all of the findAndModify commands are issued with identical match criteria, say

{ name: "andy" }

. Suppose that initially, no document with

{ name : "andy" }

exists, and there is no unique index on the "name" field. In this case, if several of the findAndModify commands complete the "find" phase of the operation before any engage in the "modify" phase, those instances of the command may all choose to perform an insert of a new document, since no matching document was found. This is the same as the behavior of "update" operations with upsert behavior.

In the above example, had there been a unique index on the "name" field, each fAndM command instance would have seen one of the following behaviors:

  • Exactly one fAndM execution would successfully insert a new document.
  • Zero or more fAndMs would update the document inserted by the inserter.
  • Zero or more fAndMs would fail when they attempted to insert a duplicate.

To implement, for example, a counter with fAndM, use a unique index on the identifying field (like the name), and if an fAndM fails due to unique index constraint violation, simply repeat it. It will not fail due to this race multiple times, unless there is a process deleting counter documents.



 Comments   
Comment by auto [ 19/Feb/13 ]

Author:

{u'date': u'2013-02-19T21:35:15Z', u'name': u'Sam Kleinman', u'email': u'samk@10gen.com'}

Message: merge: DOCS-861
Branch: master
https://github.com/mongodb/docs/commit/f1da0c28c1f357fc806d906b9db046f186108d14

Comment by auto [ 19/Feb/13 ]

Author:

{u'date': u'2013-01-15T04:40:19Z', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-861 findAndModify and upserts can insert multiple documents

DOCS-861 findandmodify upsert clarification

DOCS-861 findandmodify upsert clarification

findAndModify flesh out what is returned

DOCS-861 multiple invocation of findAndModify and flesh out results
Branch: master
https://github.com/mongodb/docs/commit/8affb07c2f969c59f3b38a4c31a5b43185def9fd

Comment by auto [ 19/Feb/13 ]

Author:

{u'date': u'2013-01-15T04:40:19Z', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-861 findAndModify and upserts can insert multiple documents

DOCS-861 findandmodify upsert clarification

DOCS-861 findandmodify upsert clarification

findAndModify flesh out what is returned

DOCS-861 multiple invocation of findAndModify and flesh out results
Branch: master
https://github.com/mongodb/docs/commit/8affb07c2f969c59f3b38a4c31a5b43185def9fd

Comment by auto [ 19/Feb/13 ]

Author:

{u'date': u'2013-01-15T04:40:19Z', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-861 findAndModify and upserts can insert multiple documents

DOCS-861 findandmodify upsert clarification

DOCS-861 findandmodify upsert clarification

findAndModify flesh out what is returned

DOCS-861 multiple invocation of findAndModify and flesh out results
Branch: master
https://github.com/mongodb/docs/commit/8affb07c2f969c59f3b38a4c31a5b43185def9fd

Comment by auto [ 19/Feb/13 ]

Author:

{u'date': u'2013-01-15T04:40:19Z', u'name': u'kay', u'email': u'kay.kim@10gen.com'}

Message: DOCS-861 findAndModify and upserts can insert multiple documents

DOCS-861 findandmodify upsert clarification

DOCS-861 findandmodify upsert clarification

findAndModify flesh out what is returned

DOCS-861 multiple invocation of findAndModify and flesh out results
Branch: master
https://github.com/mongodb/docs/commit/8affb07c2f969c59f3b38a4c31a5b43185def9fd

Generated at Thu Feb 08 07:39:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.