具有ES6模板字符串的MongoDB对象密钥 [英] MongoDB Object key with ES6 template string
本文介绍了具有ES6模板字符串的MongoDB对象密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下方法更新集合中的数组:
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}});
这是为了表明它有效,但是当我使用第一个解决方案时会抛出错误Unexpected token
.
This is to show that it works, but It's throwing an error Unexpected token
when I use the first solution.
我的问题是,如何获得作为Object键的最佳解决方案?
My question is, how can I get the top solution to work as the Object key?
推荐答案
模板文字不能用作对象文字中的键.改用计算属性:
Template literals cannot be used as key in an object literal. Use a computed property instead:
db.collection('connect').update({_id: id}, {$push: {[str]: item}});
// ^^^^^
这篇关于具有ES6模板字符串的MongoDB对象密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文