$addToSet / $push for key/value-pair

773 views
Skip to first unread message

proximus

unread,
Dec 8, 2010, 5:31:49 AM12/8/10
to mongodb-user
Hi everyone,


I am stuck in a question concerning updates on nested objects/arrays
in documents. E. g. I have a doc which looks like this:

{
"_id": ObjectId("4cff58f288a76d2e16020000"),
"ses_filter": {
"cutters": {
"0": "Substrat\/Leistungsklasse",
"1": "\u2205 Durchmesser",
"2": "Eignung",
...
}
},
...
}

What I want to do: I want to add an element with a key/value (object)
to the set "ses_filter", like this:

{
"_id": ObjectId("4cff58f288a76d2e16020000"),
"ses_filter": {
"cutters": {
"0": "Substrat\/Leistungsklasse",
"1": "\u2205 Durchmesser",
"2": "Eignung",
...
},
"drills": {
"0": "Substrat\/Leistungsklasse",
"1": "\u2205 Durchmesser",
"2": "Eignung",
...
}
},
...
}

I added an element with the key "drills" (the value of drills is
another set of elements). If "drills" already exists in the set
"ses_filter", the set of elements behind "drills" should be updated. I
tried working with $addToSet and $push, but it only works properly if
"ses_filter" is an array with numeric keys and I definitely need an
associative array.

How would you do that? Is it possible in a single update? FYI: I am
working with PHP driver.


Cheers,

Dennis

Eliot Horowitz

unread,
Dec 8, 2010, 9:43:20 AM12/8/10
to mongod...@googlegroups.com
I think you just want to do { $set : { "ses_filter.drills" : { .. } }
if I understand.

> --
> 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.
>
>

proximus

unread,
Dec 9, 2010, 4:00:15 AM12/9/10
to mongodb-user
Actually it seems as it overwrites the field "ses_filter". I made 2
updates:


Thu Dec 9 09:50:22 [conn2] update fraisa.fraisa_session query:
{ _id: ObjectId('4d00984c88a76db803000000') } update: { $set:
{ ses_filter.fraeswerkzeuge: [ "Substrat/Leistungsklasse", "∅
Durchmesser", "Eignung" ] } } byid 0ms


Thu Dec 9 09:55:00 [conn4] update fraisa.fraisa_session query:
{ _id: ObjectId('4d00995f88a76db703000000') } update: { $set:
{ ses_filter.fraeswerkzeuge_sonderformen: [ "Substrat/
Leistungsklasse", "∅ Durchmesser", "Eignung" ] } } byid 0ms


After updating the doc looks like this:


{
"_id": ObjectId("4d00995f88a76db703000000"),
"ses_filter": {
"fraeswerkzeuge_sonderformen": {
"0": "Substrat\/Leistungsklasse",
"1": "\u2205 Durchmesser",
"2": "Eignung"
}
},
...
}


The element "fraeswerkzeuge" ist missing.

Oded Maimon

unread,
Dec 9, 2010, 4:52:17 AM12/9/10
to mongod...@googlegroups.com
if you are using 1.6.4 version, 32bit linux/windows or 64bit on windows, try using 1.6.5 instead

proximus

unread,
Dec 9, 2010, 7:15:47 AM12/9/10
to mongodb-user
You might be true... I hope the update for our productive CentOS
environment is available asap... ;)

Antoine Girbal

unread,
Dec 9, 2010, 7:11:56 PM12/9/10
to mongodb-user
Are you sure that you are looking at the same object?
The _id does not match..

The following worked fine for me:
> db.proximus.save({"ses_filter": {
... "cutters": {
... "0": "Substrat\/Leistungsklasse",
... "1": "\u2205 Durchmesser",
... "2": "Eignung",
... "3": "Blah" }}})
> db.proximus.find()
{ "_id" : ObjectId("4d0165ed772d2a2e10895172"), "ses_filter" :
{ "cutters" : { "0" : "Substrat/Leistungsklasse", "1" : "∅
Durchmesser", "2" : "Eignung", "3" : "Blah" } } }
> db.proximus.update({}, {$set : {"ses_filter.drills" : {
... "0": "Substrat\/Leistungsklasse",
... "1": "\u2205 Durchmesser",
... "2": "Eignung"}}})
> db.proximus.find()
{ "_id" : ObjectId("4d0165ed772d2a2e10895172"), "ses_filter" :
{ "cutters" : { "0" : "Substrat/Leistungsklasse", "1" : "∅
Durchmesser", "2" : "Eignung", "3" : "Blah" }, "drills" : { "0" :
"Substrat/Leistungsklasse", "1" : "∅ Durchmesser", "2" :
"Eignung" } } }
> db.proximus.update({}, {$set : {"ses_filter.drills" : { "0": "new value" }}})
> db.proximus.find()
{ "_id" : ObjectId("4d0165ed772d2a2e10895172"), "ses_filter" :
{ "cutters" : { "0" : "Substrat/Leistungsklasse", "1" : "∅
Durchmesser", "2" : "Eignung", "3" : "Blah" }, "drills" : { "0" : "new
value" } } }

thx
AG

proximus

unread,
Dec 10, 2010, 5:29:12 AM12/10/10
to mongodb-user
Hi Antoine,


thanks for your important hint... actually my updating function was
buggy - took me one week to find out... :-/


Have a nice day,

Dennis
Reply all
Reply to author
Forward
0 new messages