Skip to content

Instantly share code, notes, and snippets.

View aniruddha27's full-sized avatar

Aniruddha Bhandari aniruddha27

View GitHub Profile
# Nearest locations
db.restaurants.find({'address.coord':
{'$near':[50,50],
'$minDistance':10,
'$maxDistance':100
}
}).count()
# Query geospatial index
db.restaurants.find({'address.coord':{'$near':[50,50]}}).count()
# Drop previous indexes
db.restaurants.drop_indexes()
# Create Geospatial index
db.restaurants.create_index([('address.coord', pymongo.GEO2D)],
name='coord')
# List indexes
pprint(db.restaurants.index_information())
pprint(db.restaurants.find({'grades.grade':'Z'}).explain()["executionStats"])
# Create multikey index
db.restaurants.create_index('grades.grade',
name='grade')
# List indexes
pprint(db.restaurants.index_information())
# Query stats for query to find restaurants with grade Z
pprint(db.restaurants.find({'grades.grade':'Z'}).explain()["executionStats"])
# Drop indexes
db.restaurants.drop_indexes()
{'allPlansExecution': [],
'executionStages': {'advanced': 152,
'alreadyHasObj': 0,
'docsExamined': 152,
'executionTimeMillisEstimate': 0,
'inputStage': {'advanced': 152,
'direction': 'forward',
'dupsDropped': 0,
'dupsTested': 0,
'executionTimeMillisEstimate': 0,
# Execution stats for query for all restaurants in Manhattan
pprint(db.restaurants.find({'borough':'Manhattan'}).explain()['executionStats'])
# Execution stats for query to find all Indian restaurants in Manhattan
pprint(db.restaurants.find({'borough':'Manhattan','cuisine':'Indian'}).explain()['executionStats'])