Re: [ymds] Update array of EmbeddedDocument

91 views
Skip to first unread message

Zheng Gui

unread,
Apr 19, 2012, 10:36:29 AM4/19/12
to yiimongodbsiute
There is no way to do this with YMDS. You can easily do whatever you want with the native PHP MongoCollection object, like:
 
User::model()->getCollection()->update(
array('addresses.city' => 'My City'),
array('$set' => array('addesses.$.city' => 'New York')),
array('multiple' => true)
);
 
Not tested, but it should work this way. In my experience, MongoCollection seems to be the only choice.
 
2012-04-19

Zheng Gui

发件人: Riccardo Coppola
发送时间: 2012-04-19  17:38:49
收件人: yiimongodbsiute
抄送:
主题: [ymds] Update array of EmbeddedDocument
Hi all,
I have a field in my collection that is an array of embedded documents. 
Now, I have to update an embedded document using one of it's properties as key for the query
In short, I can't use
$user->addresses[0]->city = 'New York';
but i'd like to use something like
$user->addresses['city': 'MyCity']->city = 'New York';

To update the embedded document with city = 'MyCity' with the new value 'New York'.

In mongo shell, i'd use the $ positional operator this way:
t.update( {'adresses.city':'MyCity'}, {$set:{'addresses.$.city': 'New York'}})
Is there a way to do it with YiiMongoDbSuite?
Thank you,
Riccardo

Riccardo Coppola

unread,
Apr 20, 2012, 4:11:41 AM4/20/12
to yiimong...@googlegroups.com
Thanks Zheng,
I agree.
The only thing to do is to use the native mongo driver. The other solution is to check the array for your data, but it's way too expensive for large datasets.

Thank you again,
Riccardo

Il giorno giovedì 19 aprile 2012 16:36:29 UTC+2, Zheng ha scritto:

Reply all
Reply to author
Forward
0 new messages