I want to retrieve values of only certain keys from a MongoDB collection.
But, the collection has some keys which have a 'space' in their name like:
"Parent":{"key1": //some string,
"key2": //some string,
"key 3": //some string}
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? I am using Python and PyMongo.
For normal keys I can do this:
db.coll_name.find({"key": "India"}, {"_id": 0, "Parent.key1": 1, "Parent.key2": 1})
So how do I use the key "Parent['key 3']" in the second argument of the above query? Is there any way to achieve this?
Here's the query which returns data(works):
db.coll_name.find({}, {"Parent.key1": 1, "_id": 0})
Here's the query which doesn't return data:
db.coll_name.find({}, {"Parent['key 3']": 1, "_id": 0})
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 })