Mac*_*zuk 3 javascript mongodb mongodb-query
我的集合包含空格的键,我不知道如何从js脚本访问它.例如:
c = db.collection.find()
while(c.hasNext()) {
print(c.next().'my key with spaces');
}
Run Code Online (Sandbox Code Playgroud)
不行.怎么做?
[]如果键不是有效标识符(例如包含空格)而不是点. 符号,则需要使用括号表示法来访问对象属性或文档字段.但一般来说,你应该避免使用这种标识符.
c = db.collection.find()
while(c.hasNext()) {
print(c.next()['my key with spaces']);
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用该.forEach方法而不是while循环
db.collection.find().forEach(function(document) {
print(document['my key with spaces']);
}
Run Code Online (Sandbox Code Playgroud)
或者甚至更好地使用ECMAScript 6中的箭头函数表达式
db.collection.find().forEach(document => print(document['my key with spaces']))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
539 次 |
| 最近记录: |