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