Skip to content

Instantly share code, notes, and snippets.

Avatar

Aniruddha Bhandari aniruddha27

View GitHub Profile
View mongo_stats_7.py
# Query stats
pprint(db.restaurants.find({'cuisine':'French','grades.score':{'$gt':5}}).explain()['executionStats'])
View mongo_index_13.py
# Drop previous indexes
db.restaurants.drop_indexes()
# Create Partial index
db.restaurants.create_index('cuisine',
partialFilterExpression={'grades.score':{'$gt':5}},
name='cuisine_score')
# List indexes
pprint(db.restaurants.index_information())
View mongo_index_12.py
# Create multikey index
db.restaurants.create_index([('grades.grade', pymongo.ASCENDING),
('grades.score', pymongo.ASCENDING)],
name='grade_score', unique=True)
# List indexes
pprint(db.restaurants.index_information())
View mongo_index_11.py
# Create unique Compound Index
db.restaurants.create_index([('cuisine',pymongo.ASCENDING),
('borough',pymongo.ASCENDING)],
name='cuisine_borough',
unique=True)
pprint(db.neighborhoods.index_information())
View mongo_index_10.py
# drop previous indexes
db.neighborhoods.drop_indexes()
# Create unique index
db.neighborhoods.create_index('name',
name='neighborhood',
unique=True)
# Indexes
pprint(db.neighborhoods.index_information())
View mongo_find_2.py
# Query 2dsphere index
db.neighborhoods.find({'geometry':
{'$geoWithin':
{'$geometry':
{"type":'Polygon',
'coordinates':[[[-73.89483522924053, 40.8262468412122],
[-73.89487088534919, 40.82393727215577],
[-73.89389702292222, 40.8239839983082],
[-73.89483522924053, 40.8262468412122]]]
}}}}).count()
View mongo_index_8.py
# Create Geospatial index
db.neighborhoods.create_index([('geometry', pymongo.GEOSPHERE)],
name='coordinates')
# List indexes
pprint(db.neighborhoods.index_information())
View mongo_near.py
# Nearest locations
db.restaurants.find({'address.coord':
{'$near':[50,50],
'$minDistance':10,
'$maxDistance':100
}
}).count()
View mongo_count.py
# Query geospatial index
db.restaurants.find({'address.coord':{'$near':[50,50]}}).count()
View mongo_index_7.py
# 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())