MongoDB - 使用键中的空格访问游标
MongoDB - accessing cursor with spaces in keys
我收集了包含空格的键,但我不知道如何从 js 脚本访问它。例如:
c = db.collection.find()
while(c.hasNext()) {
print(c.next().'my key with spaces');
}
无效。怎么做?
如果 keys 不是有效标识符(例如包含空格),则需要使用括号符号 []
而不是点 .
符号访问您的对象属性或文档字段。但一般来说你应该避免使用这样的标识符。
c = db.collection.find()
while(c.hasNext()) {
print(c.next()['my key with spaces']);
}
您也可以使用 .forEach
方法代替 while 循环
db.collection.find().forEach(function(document) {
print(document['my key with spaces']);
}
或者甚至更好地使用 ECMAScript 6
中的 arrow function expression 新功能
db.collection.find().forEach(document => print(document['my key with spaces']))
我收集了包含空格的键,但我不知道如何从 js 脚本访问它。例如:
c = db.collection.find()
while(c.hasNext()) {
print(c.next().'my key with spaces');
}
无效。怎么做?
如果 keys 不是有效标识符(例如包含空格),则需要使用括号符号 []
而不是点 .
符号访问您的对象属性或文档字段。但一般来说你应该避免使用这样的标识符。
c = db.collection.find()
while(c.hasNext()) {
print(c.next()['my key with spaces']);
}
您也可以使用 .forEach
方法代替 while 循环
db.collection.find().forEach(function(document) {
print(document['my key with spaces']);
}
或者甚至更好地使用 ECMAScript 6
中的 arrow function expression 新功能db.collection.find().forEach(document => print(document['my key with spaces']))