Created
August 17, 2020 19:08
-
-
Save lakshay-arora/7b821eeced11f1efa29e541c85805eeb 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
weekly_demand_collection.find_one() |
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
# importing the required libraries | |
import pymongo | |
import pprint | |
import json | |
import warnings | |
warnings.filterwarnings('ignore') | |
# connect to the mongoclient | |
client = pymongo.MongoClient('mongodb://localhost:27017') | |
# get the database | |
database = client['sample_db'] |
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
# create weekly demand collection | |
database.create_collection("weekly_demand") | |
# create meal_info collection | |
database.create_collection("meal_info") |
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
# get collection weekly_demand | |
weekly_demand_collection = database.get_collection("weekly_demand") | |
# open the weekly_demand json file | |
with open("weekly_demand.json") as f: | |
file_data = json.load(f) | |
# insert the data into the collection | |
weekly_demand_collection.insert_many(file_data) | |
# get the count of total data points | |
weekly_demand_collection.find().count() | |
# >> 456548 | |
# get collection meal_info | |
meal_info_collection = database.get_collection("meal_info") | |
# open the meal_info json file | |
with open("meal_info.json") as f: | |
file_data = json.load(f) | |
# insert the data into the collection | |
meal_info_collection.insert_many(file_data) | |
# get the count of the total data points | |
meal_info_collection.find().count() | |
# >> 51 |
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
weekly_demand_collection.find_one( {}, { "week": 1, "checkout_price" : 1}) |
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
meal_info_collection.find_one() |
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
result_7 = meal_info_collection.find({ | |
"cuisine" : { "$regex" : "^C" } | |
}) | |
for i in result_7: | |
print(i) |
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
result_8 = meal_info_collection.find({ | |
"$and" : [ | |
{ | |
"category" : { | |
"$regex" : "^S" | |
}}, | |
{ | |
"cuisine" : { | |
"$regex" : "ian$" | |
}} | |
] | |
}) | |
for i in result_8: | |
print(i) |
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
result_9 = weekly_demand_collection.aggregate([ | |
## stage 1 | |
{ | |
"$match" : | |
{"center_id" : {"$eq" : 11 } } | |
}, | |
## stage 2 | |
{ | |
"$count" : "total_rows" | |
} | |
]) | |
for i in result_9: | |
print(i) |
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
result_10 = weekly_demand_collection.aggregate([ | |
## stage 1 | |
{ | |
"$match" : | |
{"center_id" : {"$eq" : 11 } } | |
}, | |
## stage 2 | |
{ | |
"$group" : { "_id" : 0 , | |
"average_num_orders": { "$avg" : "$num_orders"}, | |
"unique_meal_id" : {"$addToSet" : "$meal_id"}} | |
} | |
]) | |
for i in result_10: | |
print(i) |
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
weekly_demand_collection.find_one( {}, {"num_orders" : 0, "meal_id" : 0}) |
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
weekly_demand_collection.find_one( {"center_id" : 55, "meal_id" : 1885}, {"_id" : 0, "week" : 0} ) |
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
result_1 = weekly_demand_collection.find({ | |
"center_id" : { "$eq" : 55}, | |
"homepage_featured" : { "$ne" : 0} | |
}) | |
for i in result_1: | |
print(i) |
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
result_2 = weekly_demand_collection.find({ | |
"center_id" : { "$in" : [ 24, 11] } | |
}) | |
for i in result_2: | |
print(i) |
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
result_3 = weekly_demand_collection.find({ | |
"center_id" : { "$nin" : [ 24, 11] } | |
}) | |
for i in result_3: | |
print(i) |
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
result_4 = weekly_demand_collection.find({ | |
"center_id" : 55, | |
"checkout_price" : { "$lt" : 200, "$gt" : 100} | |
}) | |
for i in result_4: | |
print(i) |
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
result_5 = weekly_demand_collection.find({ | |
"$and" : [{ | |
"center_id" : { "$eq" : 11} | |
}, | |
{ | |
"meal_id" : { "$ne" : 1778} | |
}] | |
}) | |
for i in result_5: | |
print(i) |
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
result_6 = weekly_demand_collection.find({ | |
"$or" : [{ | |
"center_id" : { "$eq" : 11} | |
}, | |
{ | |
"meal_id" : { "$in" : [1207, 2707]} | |
}] | |
}) | |
for i in result_6: | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment