How to combine index variable into a string?


#1

I want to update a collection like this,

Books.update({_id:bookid},{$set:{‘string.index.weight’:weight}}); / / index is a chaning variable

but it didn’t work, then I try this,

var string=‘bookitems.’+index+’.weight’; / / index is a chaning variable
Books.update({_id:bookid},{$set:{string:weight}});

it is still not work.


#2
var s = {};
s['bookitems.'+index+'.weight'] = weight;
Books.update({_id:bookid},{$set:s});

#3

@Steve is spot on.

Have a read of this blog post.
Third tip down: ‘Variables as keys’.