Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created May 18, 2021 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cankush625/860f3d4a6e8d80de829e1b93a99d6726 to your computer and use it in GitHub Desktop.
Save cankush625/860f3d4a6e8d80de829e1b93a99d6726 to your computer and use it in GitHub Desktop.
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