[SERVER-1786] $or paired with upsert does not create documents for each or clause Created: 13/Sep/10  Updated: 06/Dec/22  Resolved: 11/Aug/17

Status: Closed
Project: Core Server
Component/s: Write Ops
Affects Version/s: 1.6.2
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: jeff jenkins Assignee: Backlog - Query Team (Inactive)
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongodb 1.6.2, pymongo-1.8.1, python 2.6


Assigned Teams:
Query
Operating System: OS X
Participants:

 Description   

When multiple keys are put together with an $or expression and the upsert flag is sent to update (and the multi flag is set) objects are not created for the items in the $or expression.

This code using pymongo:

keys = []
for item in items:
key =

{ 'a : item.a, 'b' : item.b, 'c' : item.c }

keys.append(key)

if len(keys) == 0:
return

query =

{ '$or' : keys }

value = {
'$inc' :

{ 'count' : 1 }

}

db.abc_counts.update(query, value, upsert=True, multi=True)

Produces this in the DB:

{ "_id" : ObjectId("4c8e7161010f681721439e58"), "count" : 1 }

if i replace query with keys[0] it works as expected.



 Comments   
Comment by Eliot Horowitz (Inactive) [ 16/Jan/11 ]

unclear if that's the right semantics or not

Comment by jeff jenkins [ 14/Sep/10 ]

No, I was expecting each item in $or to be a new object with count:1, or to have count:n if it already existed. The use-case is that I've got 10s of objects which have a count associated with them which I would like to increment. It could be that this doesn't make sense when the $or expressions aren't uniform like mine are, but I couldn't find any other way to do a bulk upsert in pymongo. If it isn't something I can do, upsert and $or should probably be mutually exclusive and raise an exception.

I was expecting:

{ count: 1, a: 'someA', b:'someB', c:'someC', _id:ObjectId('23423423') } { count: 1, a: 'anotherA', b:'anotherB', c:'anotherC', _id:ObjectId('23423423') }

etc

Comment by Scott Hernandez (Inactive) [ 13/Sep/10 ]

Jeff,

So you expect all the of "or" conditions to be combined into an array?

So update({$or:

{x: [1,2,3])}

, {$set:{y:1}}, true, true) should result in this document?
{_id:..., x:[1,2,3], y:1}

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