Update on dot notation subdocument does a replace all

288 views
Skip to first unread message

Van

unread,
Sep 16, 2010, 2:01:47 PM9/16/10
to mongodb-user
Hi all,

We're working on a project that can't move any further because of a
strange issue we've narrowed down. I put the code in a pastebin that
illustrates whats happening in the simplest form:

http://pastebin.com/wSnTGSaD

Basically, doing a

.update(key, {$set:{"subkey.subsubkey.value":new_value}})

results in subkey.*.value being set to new_value. Is that the
expected behavior? I'm not sure if its something we're doing or
what. Please let me know if anyone has any clues as to what's wrong.
Thanks for your time.

Best Regards,
Van

PS: I have also put this in a jira ticket: http://jira.mongodb.org/browse/SERVER-1798

Dwight Merriman

unread,
Sep 16, 2010, 2:05:14 PM9/16/10
to mongod...@googlegroups.com
you can't use this syntax within the field name 

> db.invites.update({"key":"100001159387254"}, 
{"$set":
 {
  "sent['100001181664410'].to_id":"100001181664410",
  "sent.100001181664410.created_at":1284579765034}})

you probably need "sent.100001181664410.to_id" instead.  i did not read in detail but there shouldn't be braces '[' in any fields names.

let us know if that helps



--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Van

unread,
Sep 16, 2010, 2:07:32 PM9/16/10
to mongodb-user
Ah, sorry, I included that part on accident. We were trying to
experiment other ways to reproduce our issue. Above the part you
pasted,

db.invites.update({"key":"100001159387254"}, {"$set":{"sent.
100001181664410.to_id":"100001181664410","sent.
100001181664410.created_at":1284579765034}})

updates everything in the sent subdocument to those values. It
overwrote the first update/upsert's values.

On Sep 16, 11:05 am, Dwight Merriman <dwi...@10gen.com> wrote:
> you can't use this syntax within the field name
>
> > db.invites.update({"key":"100001159387254"},
>
> {"$set":
>  {
>   "*sent['100001181664410'].to_id*":"100001181664410",
>   "sent.100001181664410.created_at":1284579765034}})
>
> you probably need "sent.*100001181664410.to_id" instead.  i did not read in
> detail but there shouldn't be braces '[' in any fields names.*
> *
> *
> *let us know if that helps*
> *
> *
>
>
>
> On Thu, Sep 16, 2010 at 2:01 PM, Van <thegoleff...@gmail.com> wrote:
> > Hi all,
>
> > We're working on a project that can't move any further because of a
> > strange issue we've narrowed down.  I put the code in a pastebin that
> > illustrates whats happening in the simplest form:
>
> >http://pastebin.com/wSnTGSaD
>
> > Basically, doing a
>
> > .update(key, {$set:{"subkey.subsubkey.value":new_value}})
>
> > results in subkey.*.value being set to new_value.  Is that the
> > expected behavior?  I'm not sure if its something we're doing or
> > what.  Please let me know if anyone has any clues as to what's wrong.
> > Thanks for your time.
>
> > Best Regards,
> > Van
>
> > PS: I have also put this in a jira ticket:
> >http://jira.mongodb.org/browse/SERVER-1798
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "mongodb-user" group.
> > To post to this group, send email to mongod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > mongodb-user...@googlegroups.com<mongodb-user%2Bunsubscribe@google groups.com>
> > .

Michael Dirolf

unread,
Sep 16, 2010, 2:09:39 PM9/16/10
to mongod...@googlegroups.com
I don't see what you mean - it looks to me like it worked correctly?
If you rewrite your example w/ simpler key names (think 'a', 'b', 'c')
it might make it easier for us to follow.

> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.

Van

unread,
Sep 16, 2010, 2:14:20 PM9/16/10
to mongodb-user
Sorry, let me try again:


db.invites.update(key, {$set:{"a.b.c":1}})
db.invites.find(key)
{
"_id": Obj---,
"a": {
"b":{
"c":1
}
}
}
db.invites.find(key)
db.invites.update(key, {$set:{"a.d.c":999}})
{
"_id": Obj---,
"a": {
"b":{
"c":999
},
"d":{
"c":999
}
}
}

http://pastie.org/1163289

Is that better or no? hmm, sorry

Van

unread,
Sep 16, 2010, 2:16:00 PM9/16/10
to mongodb-user
Ugh, slightly out of order:

db.invites.update(key, {$set:{"a.b.c":1}})
db.invites.find(key)
{
"_id": Obj---,
"a": {
"b":{
"c":1
}
}
}
db.invites.update(key, {$set:{"a.d.c":999}})
db.invites.find(key)
{
"_id": Obj---,
"a": {
"b":{

Michael Dirolf

unread,
Sep 16, 2010, 2:15:56 PM9/16/10
to mongod...@googlegroups.com
What is it about this behavior that you think is wrong? That looks
correct to me.

Van

unread,
Sep 16, 2010, 2:18:34 PM9/16/10
to mongodb-user
Oh, we're trying to set "a.d"'s value of c to 999 but its also setting
"a.b"'s value of c to 999 as well.

Michael Dirolf

unread,
Sep 16, 2010, 2:21:08 PM9/16/10
to mongod...@googlegroups.com
I see now. I can't reproduce this behavior (it works correctly for
me). What version are you using? Can you try upgrading?

Van

unread,
Sep 16, 2010, 2:35:10 PM9/16/10
to mongodb-user
We're using a nightly build from August 8th, 1.6.1-pre. If I run the
simplified example above, it works as expected. So I kept tweaking it
until it got closer to the specific example that gives us the
weirdness. Could you please try this:

db.invites.remove({})
key = {"key":"1"}
db.invites.update(key, {$set:{"a.100001140891858.c":1}}, true)
db.invites.find(key)
db.invites.update(key, {$set:{"a.100001140891410.c":999}})
db.invites.find(key)

We're expecting that the second update will add a separate key/value
under a but its updating the first key's value. Here is our output:

> db.invites.remove({})
> key = {"key":"1"}
{ "key" : "1" }
> db.invites.update(key, {$set:{"a.100001140891858.c":1}}, true)
> db.invites.find(key)
{ "_id" : ObjectId("4c9263084cb16aa4fe28c0fc"), "a" :
{ "100001140891858" : { "c" : 1 } }, "key" : "1" }
> db.invites.update(key, {$set:{"a.100001140891410.c":999}}, true)
> db.invites.find(key)
{ "_id" : ObjectId("4c9263084cb16aa4fe28c0fc"), "a" :
{ "100001140891858" : { "c" : 999 } }, "key" : "1" }

There is no reference to "100001140891410" anywhere in the document
after the second update.

Michael Dirolf

unread,
Sep 16, 2010, 2:42:54 PM9/16/10
to mongod...@googlegroups.com
Can you try the latest nightly to rule out a version issue? I still
can't reproduce this using the latest code.

As a note: in general it's often not a great idea to use numeric keys
like that, due to confusion that can result between an embedded
document with numeric keys and an embedded array. So you might want to
move away from that either way.

Van

unread,
Sep 16, 2010, 2:55:16 PM9/16/10
to mongodb-user
Thanks for your help Michael. ^_^

I tested all of the above code using the latest nightly on OSX. It
works for all of those cases. I extended the length of the key in the
middle like so:

db.invites.remove({})
key = {"key":"1"}
db.invites.update(key, {$set:{"a.
100001140891858100001140891858100001140891858100001140891858.c":1}},
true)
db.invites.find(key)
db.invites.update(key, {$set:{"a.
100001140891410100001140891410100001140891410100001140891410.c":999}},
true)
db.invites.find(key)

And I'm seeing what I saw on the older version:

> db.invites.remove({})
> key = {"key":"1"}
{ "key" : "1" }
> db.invites.update(key, {$set:{"a.100001140891858100001140891858100001140891858100001140891858.c":1}}, true)
> db.invites.find(key)
{ "_id" : ObjectId("4c9266db319cc0206ae656b4"), "a" :
{ "100001140891858100001140891858100001140891858100001140891858" :
{ "c" : 1 } }, "key" : "1" }
> db.invites.update(key, {$set:{"a.100001140891410100001140891410100001140891410100001140891410.c":999}}, true)
> db.invites.find(key)
{ "_id" : ObjectId("4c9266db319cc0206ae656b4"), "a" :
{ "100001140891858100001140891858100001140891858100001140891858" :
{ "c" : 999 } }, "key" : "1" }

subkey ending in 858's value is overwritten by the 410 guy.


I definitely agree that this is probably not the best schema setup
that's being used but I don't see them switching schemas anytime soon
despite the issues.

Michael Dirolf

unread,
Sep 16, 2010, 3:02:26 PM9/16/10
to mongod...@googlegroups.com
I'm still not able to reproduce this, even with the longer keys...

Van

unread,
Sep 16, 2010, 3:28:36 PM9/16/10
to mongodb-user
Hi Michael,

Can you post the results from your mongo console? All the devs here
tested with the latest nightly for OSX 64-bit and are seeing the issue
with the long numeric keys.
> >> db.invites.update(key, {$set:{"a.100001140891410100001140891410100001140891410100001140891410.c":9 99}}, true)

Van

unread,
Sep 16, 2010, 3:30:26 PM9/16/10
to mongodb-user
We all just ran the same code using the online shell on the mongodb
website and did not experience the issue. So maybe the latest nightly
of OSX 64 bit isn't updated to match? I'm about to test the linux one
now.
> >> db.invites.update(key, {$set:{"a.100001140891410100001140891410100001140891410100001140891410.c":9 99}}, true)

Van

unread,
Sep 16, 2010, 3:37:34 PM9/16/10
to mongodb-user
I tried linux 32 & 64 bit (on CentOs & Ubuntu respectively), latest
nightly's and got the same result that we got on OSX 64bit.
> > >> > To post to this group, send email to...
>
> read more »

pingw33n

unread,
Sep 16, 2010, 4:11:56 PM9/16/10
to mongodb-user
I can confirm that behavior on 1.6.2 x64 (windows 6, 1, 7600, 2, '').
Looks like it's int32 overflow: http://gist.github.com/583079

pingw33n

unread,
Sep 16, 2010, 4:14:24 PM9/16/10
to mongodb-user
I can confirm that behavior on 1.6.2 x64 (windows 6, 1, 7600, 2, '').
Looks like it's int32 overflow: http://gist.github.com/583079

On Sep 16, 9:35 pm, Van <thegoleff...@gmail.com> wrote:

Michael Dirolf

unread,
Sep 16, 2010, 4:16:17 PM9/16/10
to mongod...@googlegroups.com
Okay, can you file a case on jira.mongodb.org about this? TBH the best
bet for you is probably going to be to avoid using integer key names.

Van

unread,
Sep 16, 2010, 4:23:40 PM9/16/10
to mongodb-user
I remade a jira ticket specifically for this issue.

http://jira.mongodb.org/browse/SERVER-1805
Reply all
Reply to author
Forward
0 new messages