Updating a subobject in an array

378 views
Skip to first unread message

Dan Dart

unread,
Sep 2, 2011, 9:07:19 AM9/2/11
to mongod...@googlegroups.com
Hi all,

I have something like:

[
{
"_id" : "Person1",
"Age" : "20",
"Name" : "Dan",
"Hobbies" : [
{
"CId" : "42485756",
"What" : "Climbing",
"Where" : "Manchester"
},
{
"CId" : "00907616",
"What" : "Caving",
"Where" : "Dover"
}
]
}
]

Is there a command I can run to:

a) Change "Where" to "Folkestone" where _id is Person1, and
Hobbies.CId is 00907616
b) Add "What" as "UFO Sightseeing" in a new subobject where
Hobbies.CId is 24429897

and can the same command do both?

Cheers

Sam Millman

unread,
Sep 2, 2011, 9:14:45 AM9/2/11
to mongod...@googlegroups.com
A) yea:

find({ "_id": "Person1", "Hobbies.Cld" : 00907616}, {"Hobbies.$.Where": "Folkestone"})

B) Just change the above query to match the different fields


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


Sam Millman

unread,
Sep 2, 2011, 9:15:09 AM9/2/11
to mongod...@googlegroups.com
Whoops just realised it is not the find() function but more the update() function.

Sorry about that

Sam Millman

unread,
Sep 2, 2011, 9:15:45 AM9/2/11
to mongod...@googlegroups.com
"and can the same command do both?"

Not the same command, only two different commands

Dan Dart

unread,
Sep 2, 2011, 9:25:52 AM9/2/11
to mongod...@googlegroups.com
I figured out some things by using:

update({ "_id": "Person1", "Hobbies.Cld" : 00907616},
{"$set":{"Hobbies.$.Where": "Folkestone"}})

However, can I do this to update an entire person at once?
Say I wanted to update the age, name, add a surname, etc. I could do
that all in one command using $set.

Can I use one command to update the name, AND a hobby and possibly
another index child?

Sam Millman

unread,
Sep 2, 2011, 10:35:38 AM9/2/11
to mongod...@googlegroups.com
"Can I use one command to update the name, AND a hobby and possibly
another index child"

You should be able to use positional op and top level doc at the same time.

As for writing to another index; it all depends on how you are wishing to ascertain that index.

I think (I am not sure since I have never had to do it) you can do hobbie.0.cld while doing hobbies.$.cld though only way to find out is to test :).


--

Dan Dart

unread,
Sep 2, 2011, 11:47:41 AM9/2/11
to mongod...@googlegroups.com
> I think (I am not sure since I have never had to do it) you can do
> hobbie.0.cld while doing hobbies.$.cld though only way to find out is to
> test :).

I know for sure I can't push one and update one.
I'm finding it difficult to update two in different indexes since $ is
only for one query...

Sam Millman

unread,
Sep 2, 2011, 12:11:20 PM9/2/11
to mongod...@googlegroups.com
Yes pushing and updating has to be two different queries. I probably mis read what you wrote.


--

Marc

unread,
Sep 2, 2011, 3:50:08 PM9/2/11
to mongodb-user
Given this example document, it is possible to update the age and name
of Person1, add a surname, and add another index child like so:

> db.hobbies.update({ "_id": "Person1", "Hobbies.CId" : "00907616"}, {"$set":{"Hobbies.$.Where": "SomewhereElse", "Surname":"Smith", "age":"21", "Hobbies.$.When": "9/2/11"}})

Adding another document to the Hobbies array may be done with the
$push command, or with the $addToSet command ($addToSet will prevent
duplicate documents from being added):

> db.hobbies.update({"_id": "Person1"}, {$push:{"Hobbies":{"CId" : "12345678", "What" : "Biking", "Where" : "Folkestone"}}})

or

> db.hobbies.update({"_id": "Person1"}, {$addToSet:{"Hobbies":{"CId" : "12345678", "What" : "Biking", "Where" : "Folkestone"}}})

Unfortunately, it is not possible to update or add keys and add
another document to the "Hobbies" array in one operation. This will
result in the error message: "have conflicting mods in update"

> db.hobbies.update({ "_id": "Person1", "Hobbies.CId" : "00907616"}, {$addToSet:{"Hobbies":{"CId" : "00000123", "What" : "Running", "Where" : "Manchester"}}, "$set":{"Hobbies.$.Where": "Another Place", "Surname":"Smith", "age":"22", "Hobbies.$.When": "9/3/11"}})
have conflicting mods in update
Reply all
Reply to author
Forward
0 new messages