Created
May 18, 2021 08:04
-
-
Save cankush625/860f3d4a6e8d80de829e1b93a99d6726 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pymongo | |
from bson.code import Code | |
myclient = pymongo.MongoClient("mongodb://localhost:27017/") | |
mydb = myclient["mydb"] | |
mycol = mydb["myColl"] | |
mylist = [ | |
{ "resource": "oxygen", "city": "Mumbai", "status": "verified"}, | |
{ "resource": "oxygen", "city": "Jaipur", "status": "verified"}, | |
{ "resource": "plasma", "city": "delhi", "status": "not verified"}, | |
{ "resource": "remdesivir", "city": "Jaipur", "status": "verified"}, | |
{ "resource": "plasma", "city": "Banglore", "status": "not verified"}, | |
{ "resource": "ICU Bed", "city": "Mumbai", "status": "verified"}, | |
{ "resource": "oxygen", "city": "Pune", "status": "not verified"} | |
] | |
x = mycol.insert_many(mylist) | |
print(x.inserted_ids) | |
for x in mycol.find(): | |
print(x) | |
map1 = Code("function() { emit(this.resource, 1);}") | |
reduce1 = Code("function(key, values) {return Array.sum(values)}") | |
result = mycol.map_reduce(map1, reduce1, "myresult") | |
newcol = mydb["myresult"] | |
for y in newcol.find(): | |
print(y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment