Skip to content

Instantly share code, notes, and snippets.

@cankush625
Created May 18, 2021 08:04
Show Gist options
  • Select an option

  • Save cankush625/860f3d4a6e8d80de829e1b93a99d6726 to your computer and use it in GitHub Desktop.

Select an option

Save cankush625/860f3d4a6e8d80de829e1b93a99d6726 to your computer and use it in GitHub Desktop.

Revisions

  1. cankush625 created this gist May 18, 2021.
    30 changes: 30 additions & 0 deletions mongo_mapred.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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)