MongoDB: Query a key having space in its name

↘锁芯ラ 提交于 2019-12-05 01:32:40

Well the only way you could have constructed this is like:

content = {};
content["Parent"] = {}
content["Parent"]["key2"] = 1
content["Parent"]["key 3"] = 1

db.coll_name.insert(content)

But you seem to be missing that there is nothing wrong with doing this:

db.coll_name.find({ "Parent.key 3":  1} )

Or in projection

 db.coll_name.find({}, { "Parent.key 3": 1 })

It's "dot notation" and not object notation, and as long as you quote the key names ( which is mandatory for dot notation ) then all it fine and you can have a space in there.

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?

What I will suggest is:

  • Remove space from documents key using bulk write operations

    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()
    
  • Then your projection become simpler

    db.coll_name.find({'key': 'India'}, {'_id': 0, 'Parent.key1': 1, 'Parent.key2': 1, 'Parent.key3': 1 })
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!