Positional updates wrong when multiple array fields are used in the query

36 views
Skip to first unread message

Steve

unread,
Mar 9, 2012, 3:29:04 PM3/9/12
to mongodb-user
I've noticed what I believe to be a bug in the way the positional
operator works. Consider the following simple collection:

test 12:12:10.506> db.posTest.save( { 'x' : 1, 'ary' : [ { 'a' : 1,
'b' : 0 }, { 'a' : 2, 'b' : 0 }, { 'a' : 3, 'b' : 0 }, { 'a' : 3,
'b' : 1 } ] } )
test 12:12:47.833> db.posTest.find().pretty()
{
"_id" : ObjectId("4f5a643fb9883f362a5efa4f"),
"x" : 1,
"ary" : [
{
"a" : 1,
"b" : 0
},
{
"a" : 2,
"b" : 0
},
{
"a" : 3,
"b" : 0
},
{
"a" : 3,
"b" : 1
}
]
}

I attempt the following update:

test 12:12:51.861> db.posTest.update( { 'x' : 1, 'ary.a' : 2,
'ary.b' : 0 }, { $set : { 'ary.$.b' : 1 } } );
test 12:13:01.226> db.posTest.find().pretty()
{
"_id" : ObjectId("4f5a643fb9883f362a5efa4f"),
"x" : 1,
"ary" : [
{
"a" : 1,
"b" : 1
},
{
"a" : 2,
"b" : 0
},
{
"a" : 3,
"b" : 0
},
{
"a" : 3,
"b" : 1
}
]
}


I would expect this to update the 2nd element of the array to { "a" :
2, "b" : 1 } since that is the first array element to match the query
in the update. It seems, however, that the positional operator is set
to the first element in the array that matches the last array element
in the query (in this case, the first element with "b" : 0, which was
{ "a" : 1, "b" : 0}. This assumption is seen in the following as
well...

test 12:13:03.198> db.posTest.update( { 'x' : 1, 'ary.b' : 1,
'ary.a' : 3 }, { $set : { 'ary.$.a' : 4 } } );
test 12:14:12.849> db.posTest.find().pretty()
{
"_id" : ObjectId("4f5a643fb9883f362a5efa4f"),
"x" : 1,
"ary" : [
{
"a" : 1,
"b" : 1
},
{
"a" : 2,
"b" : 0
},
{
"a" : 4,
"b" : 0
},
{
"a" : 3,
"b" : 1
}
]
}

I'm using MongoDB v2.0.0 on windows server 2008 r2. This certainly
seems like a bug to me; I don't recall seeing any limitations in the
documentation regarding the query for a positional update.

Thanks,

Steve

Kyle Banker

unread,
Mar 9, 2012, 5:59:38 PM3/9/12
to mongodb-user
This might indeed be a bug. I'm going to post your example as a server
ticket, and we can continue the conversation there:
https://jira.mongodb.org/browse/SERVER-5276
Reply all
Reply to author
Forward
0 new messages