Skip to content

Instantly share code, notes, and snippets.

View aniruddha27's full-sized avatar

Aniruddha Bhandari aniruddha27

View GitHub Profile
# Drop indexes
db.restaurants.drop_indexes()
# Create text index
db.restaurants.create_index([('name', 'text')],
name='restaurant_name')
# List indexes
pprint(db.restaurants.index_information())
# Query stats
pprint(db.restaurants.find({'cuisine':'French'}).explain()['executionStats'])
# Query stats
pprint(db.restaurants.find({'cuisine':'French','grades.score':{'$gt':2}}).explain()['executionStats'])
# Query stats
pprint(db.restaurants.find({'cuisine':'French','grades.score':{'$gt':5}}).explain()['executionStats'])
# 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())
# 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())
# Create unique Compound Index
db.restaurants.create_index([('cuisine',pymongo.ASCENDING),
('borough',pymongo.ASCENDING)],
name='cuisine_borough',
unique=True)
pprint(db.neighborhoods.index_information())
# 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())
# 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()
# Create Geospatial index
db.neighborhoods.create_index([('geometry', pymongo.GEOSPHERE)],
name='coordinates')
# List indexes
pprint(db.neighborhoods.index_information())