Home > Net >  how do i print a value of a document in pymongo
how do i print a value of a document in pymongo

Time:11-02

ok suppose a doc looks like this:

{
    _id: ObjectId("63623815660b1cbf9d151a55"),
    name: 'Charmander',
    pokedex_id: '004',
    type: 'Fire',
    stats: [ { health: 10 }, { power: 15 }, { defense: 7 } ],
    moves: [ 'ember', 'growl', 'n/a', 'n/a' ]
  }

i want to get only the name element of the document to store in a variable in pymongo this is the code i am using

pokemon_python = client.pokemon_python
gonestarter = pokemon_python.gonestarter

char_name = gonestarter.find_one({"stats":{"$elemMatch":{"health": 10}}}, {"name"})
print(str(char_name))

the output is: {'_id': ObjectId('63623815660b1cbf9d151a55'), 'name': 'Charmander'}

but the desired output i want is that it prints only Charmander: Charmander

CodePudding user response:

try:

  print(str(char_name['name']))
  • Related