I'm trying to prepare the data for add an element into the nested array in MongoDB. I'm doing this is typescript:
var data = {$push:{"foo.12.bar":{
prop1: prop1,
prop2: prop2, // the right hand side values identified above the code
...
}}};
When I write this "foo.12.bar", it works fine. But what I want to do is putting this "foo.12.bar" value in a variable and makes it changeable.
This didn't work:
var propString = "foo.12.bar";
var data = {$push:{propString:{
prop1: prop1,
prop2: prop2, // the right hand side values identified above the code
...
}}};
I'm not able to write any variable name after the $push property. How can I solve that ?
You have to use bracket notation for that.
var propString = "foo.12.bar";
var data = {$push:{}};
data[propString] = {
prop1: prop1,
prop2: prop2
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.