import pymongo
mongoclient = pymongo.MongoClient()
mongoclient.mydb.mycollection.save({
'name' : '売り上げ記録1',
'items' : {
'リンゴ' : 3,
'オレンジ' : 2
}
})
mongoclient = pymongo.MongoClient()
mongoclient.mydb.mycollection.save({
'name' : '売り上げ記録1',
'items' : {
'リンゴ' : 3,
'オレンジ' : 2
}
})
mongoclient.mydb.mycollection.save({
'name' : '売り上げ記録2',
'items' : {
'リンゴ': 2,
'オレンジ' : 4,
'桃' : 5
}
})
mongoclient.mydb.mycollection.count()
2
cursor = mongoclient.mydb.mycollection.find()
for data in cursor:
data
items = {}
cursor = mongoclient.mydb.mycollection.find()
for data in cursor:
for a_item_name in data['items']:
if (a_item_name in items) is False:
items[a_item_name] = 0
items[a_item_name] += data['items'][a_item_name]
cursor = mongoclient.mydb.mycollection.find()
for data in cursor:
for a_item_name in data['items']:
if (a_item_name in items) is False:
items[a_item_name] = 0
items[a_item_name] += data['items'][a_item_name]
print(items)
結果
{'リンゴ': 5, 'オレンジ': 6, '桃': 5}