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})
The official definition of "namespace" is here: The canonical name for a collection or index in MongoDB. The namespace is a combination of the database name and the name of the collection or index, like so: [database-name]. [collection-or-index-name]. All documents belong to a namespace.
The character(s) to trim from input . The argument can be any valid expression that resolves to a string. The $trim operator breaks down the string into individual UTF code point to trim from input . If unspecified, $trim removes whitespace characters, including the null character.
Database names and Collection names are case sensitive. You can always recreate the DB/Collection with the appropriate name. The Mongo Shell is a interactive JS interpreter. and because JS is case sensitive then the shell is.
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 })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With