最近开始使用mongodb,用了一段时间后,发现有索引的需求。就把学习和使用过程写下来。先使用mongodb的shell模式,然后再用python通过pymongo使用来试试。
新建索引
db.collection.createIndex(<keyand index type specification>,<options> )
如:db.collection.createIndex( {user_id:1 })
索引类型先不展开。
升序和降序
升序为1,降序为-1,如{datetime:1}
唯一性
db.collection.createIndex( {user_id:1}, { unique: true } )
删除索引
db.accounts.dropIndex( { "user_id": 1 } )
修改索引:
db.accounts.reIndex()
修改的话还没太看明白,先不要用