yusha uzumo
yusha uzumo

Reputation: 221

Is there a way to add a new field to a collection dynamically in meteor

Suppose you have a collection called bar in a MongoDB, which has the following items :

"items" : [
    {
        "item_name" : "my_item_one",
        "price" : 20
    },
    {
        "item_name" : "my_item_two",
        "price" : 50
    },
    {
        "item_name" : "my_item_three",
        "price" : 30
    }
]

How can i add a new field called discount to each entry in the items array? I tried this without luck :

var dynamicItem = "items.$.discount"
    Bar.update( {user_id : 123456} , {$set : {dynamicItem : 5} })

Upvotes: 0

Views: 650

Answers (1)

tomsp
tomsp

Reputation: 1807

the first one is not possible at the moment, see this answer.

for the second one, try this:

var dynamicItem = {};
dynamicItem["itemsCollectionName"] = "Beer pack";

Bar.update( {user_id : 123456} , {$set : dynamicItem })

Upvotes: 1

Related Questions