gabrjan
gabrjan

Reputation: 3049

How to search in array in mongodb with zend framework

I have a base in mongo and there I have a collection that looks like that:

 array (
'list' => 
 array (
  0 => '51af6f4c4b6061e10a000164',
  1 => '51af6f4c4b6061e10a000165',
 ),
 'user_id' => '522d6a134b6061b67100000f',

)

In list array I have a list of all id's user is friend with. The problem is that I have to change all id's in base with another. Lets say I want to change all 51af6f4c4b6061e10a000165 with 51af6f4c4b6061e10a000166 in array list. How can I do that in whole collection in all lists.

Any ideas can help!

I'm realy not so used to mongo, comparing to mysql.

Upvotes: 0

Views: 79

Answers (1)

attish
attish

Reputation: 3150

Check the documentation here : DOC or here DOC

What you have to do is use positional operator with a multy:true option for the update: Something like:

db.collection.update({list:'51af6f4c4b6061e10a000165'},{$set:{'list.$':'51af6f4c4b6061e10a000166'}},{ multi: true })

Works only with two limitations, first if the new id already was in the list it will be there twice after. If the old id is multiply times in the list this will change only the first occurance.

Upvotes: 1

Related Questions