2

I have document with fields: a, b, c, d

Currently if I iterate over the collection with such documents, I get the full dictionary as output. Is there a way to get output in a key value pair with value of b as key and rest of the information as value, for example:

Suppose, there is a document: {'a':1,'b':2,'c':3,'d':4} then output should be:

{2:{'a':1,'c':3,'d':4}}
salmanwahed
  • 9,450
  • 7
  • 32
  • 55
Anuj
  • 1,160
  • 2
  • 20
  • 40

1 Answers1

1

You can approach like this,

for itm in db.collection.find():
    print {itm.pop('b'): itm}

Here collection is the name of your collection in the database. If you iterate over the pymongo cursor object you will get dict type object which you can modify like normal python dict.

salmanwahed
  • 9,450
  • 7
  • 32
  • 55