obtaining the index of a sub object to update a sub value

12 views
Skip to first unread message

Bart

unread,
Feb 8, 2010, 1:37:23 PM2/8/10
to mongodb-user
I trying to update a field in a sub document in an array.

{
'_id':'001',
'name':'Commitee One',
'members':[
{ '_id':'101', 'name':'Freddy' },
{ '_id':'102', 'name':'Barney' },
]
}
{
'_id':'002',
'name':'Commitee Two',
'members':[
{ '_id':'103', 'name':'Wilma' },
{ '_id':'101', 'name':'Freddy' },
]
}

db.test.find({'members.name':'Freddy'}) will return both committees.
( overstating the obvious... )

If I want to update the name Freddy to Fred... not replace the whole
sub document, just $set the name ( am using php, if that matters )

$groups->update( array( 'members.name'=>'Freddy' ), array( '$set' =>
array("members.1" => array("name" => "Fred"))) )
won't work. Fred is at index 0 for the first document, and index 1 of
the second.

Can I target multiple sub objects in multiple records in one update ?

Can I directly obtain the array index of a sub document?


Dwight Merriman

unread,
Feb 12, 2010, 7:26:47 AM2/12/10
to mongod...@googlegroups.com
there isn't a good way to do this with update at the moment.

this suggests that adding a new operator for cases like this would be good, i will add to jira as a feature request you can vote for it: http://jira.mongodb.org/browse/SERVER-631

in the meantime something like db.eval() may be the way to go or just multiple client/server turnarounds




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


Reply all
Reply to author
Forward
0 new messages