I'm working with jupyter notebook, pymongo. and I'm trying to display my results using format.
Here's an example of my collection user
"_id": {
"$oid": "61bd0b558659f89f7e5b1c56"
},
"first_name": "Brandise",
"last_name": "Ingerman",
"email": "bingerman0@youku.com",
"gender": "Female",
"address": {
"city": "Fresno",
"state": "California",
"country": "United States",
"country_code": "US"
},
"card": {
"card_number": "3571237735836521",
"card_type": "jcb",
"currency_code": "USD",
"balance": 630.16
},
"married_status": "true"
and here's the query that I'm executing
pipeline = [
{
"$match":{
"card.card_type": "jcb"
}
},
{
"$sort":{
"card.balance":-1
}
}
]
results = users.aggregate(pipeline)
for user in results:
print(" * user name: {first_name}, card number: {card_number}, balance: {balance}".format(
first_name=user["first_name"],
card_number=user["card.card_number"],
balance=user["card.balance"],
))
It says
15 print(" * user name: {first_name}, card number: {card_number}, balance: {balance}".format(
16 first_name=user["first_name"],
---> 17 card_number=user["card.card_number"],
18 balance=user["card.balance"],
19 ))
KeyError: 'card.card_number'
I've tried calling card_number but it keeps prompting errors, couldn't figure out how.