When update nested collection in meteor, how to pass variable for the update path

后端 未结 1 1218
无人共我
无人共我 2021-01-03 06:30

Update a nested collection in meteor is not a problem (and it is described here : Updating a nested property on a collection object with $set)

Basic way to do it :

相关标签:
1条回答
  • 2021-01-03 07:00

    It doesn't work because you can't use dynamic keys for object literals in javascript. You need to use bracket notation and build up the object you want to use in the update. For example:

    var city = 'NYC';
    var object = {};
    object["address." + city] = 'new address';
    
    MyCollectionName.update(idOfObjectToUpdate, {$set: object});
    

    Your last example should work, assuming Collection is actually the name of a collection, and {sel} is the correct selector for what you are trying to do.

    0 讨论(0)
提交回复
热议问题