php - Update a field in mongodb document stored in a array of array in yii2 model -


i have database in mongodb restaurants. sample database follows.

{    "restaurantid":"1",    "menu":    {         "menucategory":         [             {                 "menucategoryid":"12"                 "menuitems":                 [                     {                         "menuid":"123",                         "likescount":0                     },                     {                         "menuid":"1234",                         "likescount":0                     }                 ]             },             {                 "menucategoryid":"1290"                 "menuitems":                 [                     {                         "menuid":"2222",                         "likescount":0                     },                     {                         "menuid":"3333",                         "likescount":0                     }                 ]             }         ]    } } 

in document, have either increase or decrease likes count against menuid , restaurantid received in yii2. not able find likes count particular menuid in restaurant update it.

i tried following query update likescount.

db.restaurants.update({$and:[{"restaurantid":"1"},{"menu.menucategory.menu.menuid":"123"}]}, {$set:{"likescount":5}}) 

this query may you:-

db.restaurants.update( {$and:[{"restaurantid":"1"}, {"menu.menucategory.menuitems.menuid":"123"}]},  {$set:{"menu.menucategory.0.menuitems.0.likescount":5}}) 

edit

try below:-

 db.restaurants.find(  {"restaurantid":"1","menu.menucategory.menuitems.menuid":"123"}).toarray( function(err, result) {     var index,found = false;   for(var j in result[0].menu.menucategory){     for(var in result[0].menu.menucategory[j].menuitems)     {          if(results[0].menu.menucategory[j].menuitems[i].menuid == "123")          {             found = true;             index = i;             break;         }        }}    if(found)   {        var x = 'menu.menucategory.$.menuitems.'+index+'.likes';         var  updateobject = {$set : {}};            updateobject['$set'][x] = 5;        db.restaurants.update(     {"restaurantid":"1",    "menu.menucategory.menuitems.menuid":"123"}, updateobject);   } }); 

see if can you.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -