[DOCS-9971] Document breaking change for single-element $in with upsert in 3.4 release notes Created: 06/Mar/17  Updated: 26/Jul/18  Resolved: 31/Jan/18

Status: Closed
Project: Documentation
Component/s: Rel Notes
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: David Storch Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
documents SERVER-27707 findAndModify breaks when I use the q... Closed
Participants:
Days since reply: 6 years, 2 weeks, 2 days ago

 Description   

In versions prior to 3.4, it was possible to issue a single-element $in query with {upsert: true} as shown below:

> db.c.drop()
false
> db.c.update({a: {$in: [1]}}, {$addToSet: {a: 2}}, {upsert: true})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("58bdb00eb39e8f87607e9222")
})
> db.c.find()
{ "_id" : ObjectId("58bdb00eb39e8f87607e9222"), "a" : [ 2 ] }

This is no longer possible in 3.4:

> db.c.drop()
true
> db.c.update({a: {$in: [1]}}, {$addToSet: {a: 2}}, {upsert: true})
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 0,
	"nModified" : 0,
	"writeError" : {
		"code" : 16836,
		"errmsg" : "Cannot apply $addToSet to a non-array field. Field named 'a' has a non-array type double in the document INVALID-MUTABLE-ELEMENT"
	}
})

This is a breaking change which should be added to the section on "Compatibility Changes in MongoDB 3.4": https://docs.mongodb.com/manual/release-notes/3.4-compatibility/.

The reasoning is that single-element $in predicates are logically equivalent to equality predicates. Therefore, like any equality predicate, a single-element $in should seed the document being inserted when an upsert matches no documents. In the example above, the predicate {a: {$in: [1]}} seeds the document to insert with the fields {a: 1}. We then attempt to apply the $addToSet predicate to this document, which is invalid since field a does not contain an array. Users which want this behavior must wrap $in inside an $elemMatch:

> db.c.drop();
true
> db.c.update({a: {$elemMatch: {$in: [2]}}}, {$addToSet: {a: 3}}, {upsert: true});
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"nModified" : 0,
	"_id" : ObjectId("58bda2706b1609466de3d5dc")
})
> db.c.find();
{ "_id" : ObjectId("58bda2706b1609466de3d5dc"), "a" : [ 3 ] }

See SERVER-27707 for further details. I'd be happy to review a draft of the new documentation.



 Comments   
Comment by Gregory McKeon (Inactive) [ 29/Jan/18 ]

kay.kim assigning to you to clear Pavi's queue.

Comment by Githook User [ 19/Sep/17 ]

Author:

{'email': 'pavi@MacBook-Pro-77.local', 'name': 'Pavithra Vetriselvan'}

Message: DOCS-9971 breaking change to single elt in with upsert added to 3.4 release notes
Branch: v3.4
https://github.com/mongodb/docs/commit/9bf2836c51c27a7b272df53832e7aa0effecfaba

Comment by Githook User [ 19/Sep/17 ]

Author:

{'email': 'pavi@MacBook-Pro-77.local', 'name': 'Pavithra Vetriselvan'}

Message: DOCS-9971 breaking change to single elt in with upsert added to 3.4 release notes
Branch: master
https://github.com/mongodb/docs/commit/e0b0c9266523fcfa14d14c4152d84854123c8654

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