MongoDB:查询名称中包含space的键
MongoDB: Query a key having space in its name
我只想从 MongoDB 集合中检索某些键的值。
但是,该集合中有一些键的名称中带有 'space',例如:
"Parent":{"key1": //some string,
"key2": //some string,
"key 3": //some string}
我知道这是一种错误的方法,因为理想情况下键名中不应该有空格,但是我该如何查询这个键呢?我正在使用 Python 和 PyMongo。
对于普通键,我可以这样做:
db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1})
那么如何在上述查询的第二个参数中使用键 "Parent['key 3']" 呢?有什么办法可以实现吗?
这是 returns 数据(有效)的查询:
db.coll_name.find({}, {"Parent.key1": 1, "_id": 0})
这是没有 return 数据的查询:
db.coll_name.find({}, {"Parent['key 3']": 1, "_id": 0})
好吧,你构建它的唯一方法如下:
content = {};
content["Parent"] = {}
content["Parent"]["key2"] = 1
content["Parent"]["key 3"] = 1
db.coll_name.insert(content)
但是你似乎忽略了这样做没有错:
db.coll_name.find({ "Parent.key 3": 1} )
或投影
db.coll_name.find({}, { "Parent.key 3": 1 })
它是 "dot notation" 而不是对象表示法,只要您引用键名(对于点表示法是强制性的)就可以了,您可以在其中包含一个 space。
I know this is a wrong approach as there shouldn't ideally be spaces in a key name but nevertheless how do I query this key?
我的建议是:
- 从文档密钥中删除 space
bulk = coll_name.initialize_unordered_bulk_op()
count = 1000
for doc in coll_name.find():
parent = {}
parent.setdefault('Parent', {})
for key, val in doc['Parent'].items():
parent['Parent'][key.replace(' ', '')] = val
bulk.find({'_id': doc['_id']}).update({'$set': parent})
count += 1
if count % 1000 == 0:
# Execute per 1000 operations and re-init.
bulk.execute()
bulk = coll_name.initialize_unordered_bulk_op()
# Clean up queues
if count % 1000 != 0:
bulk.execute()
那么你的投影就变得更简单了
db.coll_name.find({'key': 'India'}, {'_id': 0, 'Parent.key1': 1, 'Parent.key2': 1, 'Parent.key3': 1 })
我只想从 MongoDB 集合中检索某些键的值。
但是,该集合中有一些键的名称中带有 'space',例如:
"Parent":{"key1": //some string,
"key2": //some string,
"key 3": //some string}
我知道这是一种错误的方法,因为理想情况下键名中不应该有空格,但是我该如何查询这个键呢?我正在使用 Python 和 PyMongo。
对于普通键,我可以这样做:
db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1})
那么如何在上述查询的第二个参数中使用键 "Parent['key 3']" 呢?有什么办法可以实现吗?
这是 returns 数据(有效)的查询:
db.coll_name.find({}, {"Parent.key1": 1, "_id": 0})
这是没有 return 数据的查询:
db.coll_name.find({}, {"Parent['key 3']": 1, "_id": 0})
好吧,你构建它的唯一方法如下:
content = {};
content["Parent"] = {}
content["Parent"]["key2"] = 1
content["Parent"]["key 3"] = 1
db.coll_name.insert(content)
但是你似乎忽略了这样做没有错:
db.coll_name.find({ "Parent.key 3": 1} )
或投影
db.coll_name.find({}, { "Parent.key 3": 1 })
它是 "dot notation" 而不是对象表示法,只要您引用键名(对于点表示法是强制性的)就可以了,您可以在其中包含一个 space。
I know this is a wrong approach as there shouldn't ideally be spaces in a key name but nevertheless how do I query this key?
我的建议是:
- 从文档密钥中删除 space
bulk = coll_name.initialize_unordered_bulk_op() count = 1000 for doc in coll_name.find(): parent = {} parent.setdefault('Parent', {}) for key, val in doc['Parent'].items(): parent['Parent'][key.replace(' ', '')] = val bulk.find({'_id': doc['_id']}).update({'$set': parent}) count += 1 if count % 1000 == 0: # Execute per 1000 operations and re-init. bulk.execute() bulk = coll_name.initialize_unordered_bulk_op() # Clean up queues if count % 1000 != 0: bulk.execute()
那么你的投影就变得更简单了
db.coll_name.find({'key': 'India'}, {'_id': 0, 'Parent.key1': 1, 'Parent.key2': 1, 'Parent.key3': 1 })