admin管理员组文章数量:1334673
I'm trying to update an array in my collection with this:
var str = "list.0.arr";
db.collection('connect').update({_id: id}, {$push: { `${str}`: item}});
This exact string works just fine if I do it like this:
db.collection('connect').update({_id: id}, {$push: { "list.0.arr": item}});
This is to show that it works, but It's throwing an error Unexpected token
when I use the first solution.
My question is, how can I get the top solution to work as the Object key?
I'm trying to update an array in my collection with this:
var str = "list.0.arr";
db.collection('connect').update({_id: id}, {$push: { `${str}`: item}});
This exact string works just fine if I do it like this:
db.collection('connect').update({_id: id}, {$push: { "list.0.arr": item}});
This is to show that it works, but It's throwing an error Unexpected token
when I use the first solution.
My question is, how can I get the top solution to work as the Object key?
Share Improve this question asked Jun 21, 2015 at 21:12 Ajax jQueryAjax jQuery 3452 gold badges5 silver badges19 bronze badges2 Answers
Reset to default 6Template literals cannot be used as key in an object literal. Use a puted property instead:
db.collection('connect').update({_id: id}, {$push: {[str]: item}});
// ^^^^^
See also Using a variable for a key in a JavaScript object literal
Create the update document with the string as key prior to using it in the update:
var str = "list.0.arr",
query = { "_id": id },
update = { "$push": {} };
update["$push"][str] = item;
db.collection('connect').update(query, update);
本文标签: javascriptMongoDB Object key with ES6 template stringStack Overflow
版权声明:本文标题:javascript - MongoDB Object key with ES6 template string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340061a2456418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论