索引操作
-
db.collection.dropIndex(index)
从集合中删除或移除指定索引。 不能删除_id
字段上的默认索引。 来源:https://www.mongodb.com/zh-cn/docs/manual/reference/method/db.collection.dropIndex/ -
db.collection.dropIndexes()
删除所有非_id
索引 -
db.collection.getIndexes()
获取索引信息
全文索引
db.collname.createIndex({"$*": "text"})
-
创建包括所有 string field 的全文索引(包括嵌入文档的)
-
db.collname.createIndex({"title": "text", "body": "text"})
-
创建全文索引
-
db.collname.createIndex({"title": "text", "body": "text"}, {"weights": {"title": 3, "body": 2}})
-
创建全文索引,带相对权重
-
db.collname.find({"$text": {"$search": "hello world"}}, {"title": 1}).limit(10)
- 搜索全文,搜索关键词是 "hello world",并且只返回
title
这个字段,返回10条数据
唯一索引
db.users.createIndex(
{"firstname" : 1},
{"unique" : true, "partialFilterExpression": { "firstname": {$exists: true } } }
);