MongoDB高级索引
在上面的文档中包含了一个名为 address 的子文档和一个名为 tags 的数组。> db.users.insert(
... {
... "address": {
... "city": "Hebei",
... "country": "China",
... "postcode": "000000"
... },
... "tags": [
... "music",
... "cricket",
... "blogs"
... ],
... "name": "bianchengbang"
... }
... )
WriteResult({ "nInserted" : 1 })
索引数组字段
假设要想根据内 tags 字段中的某个值来搜索用户文档,就需要我们为中的 tags 字段创建索引。想要在数组类型的字段上创建索引,需要为数组中的每个字段依次创建单独的索引。在下面的示例中,当我们在 tags 字段上创建索引时,MongoDB 会自动在 music、cricket 和 blogs 等值上创建单独的索引。
索引创建成功后,我们可以像下面这样来搜索中的文档:> db.users.createIndex({"tags":1})
{
"createdCollectionAutomatically" : false,
"numIndexeefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
若要验证在搜索的过程中是否正确的使用了索引,可以使用前面介绍的 explain() 方法。> db.users.find({tags:"cricket"}).pretty()
{
"_id" : ObjectId("603f571d5e514debed504a39"),
"address" : {
"city" : "Hebei",
"country" : "China",
"postcode" : "000000"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "bianchengbang"
}
若运行结果中存在 "cursor":"BtreeCursor tags_1" 则说明查询中正确使用了索引。> db.users.find({tags:"cricket"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "bianchengbang.users",
"indexFilterSet" : false,
"parsedQuery" : {
"tags" : {
"$eq" : "cricket"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"tags" : 1
},
"indexName" : "tags_1",
"iultiKey" : true,
"multiKeyPaths" : {
"tags" : [
"tags"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"tags" : [
"[\"cricket\", \"cricket\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "LAPTOP-MDE57TIS",
"port" : 27017,
"version" : "4.0.10",
"gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
},
"ok" : 1
}
索引子文档字段
假设我们要根据 address 子文档的 city、country、postcode 等字段来搜索文档,由于所有这些字段都是 address 子文档的一部分,所以我们需要为子文档上的所有字段创建索引。要在子文档的所有三个字段上创建索引,可以使用如下所示的代码:
索引创建成功后,我们就可以使用 address 子文档中的任何字段来搜索中的数据了,如下所示:> db.users.ensureIndex({"address.city":1,"address.country":1,"address.postcode":1})
{
"createdCollectionAutomatically" : false,
"numIndexeefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
另外,在查询时也可以设置多个查询条件,如下所示:> db.users.find({"address.city":"Hebei"}).pretty()
{
"_id" : ObjectId("603f571d5e514debed504a39"),
"address" : {
"city" : "Hebei",
"country" : "China",
"postcode" : "000000"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "bianchengbang"
}
> db.users.find({"address.city":"Hebei", "address.country":"China"}).pretty()
{
"_id" : ObjectId("603f571d5e514debed504a39"),
"address" : {
"city" : "Hebei",
"country" : "China",
"postcode" : "000000"
},
"tags" : [
"music",
"cricket",
"blogs"
],
"name" : "bianchengbang"
}
- 随机文章
- 风儿(风儿轻轻的吹)
- 饿了么红包怎么用(饿了么红包怎么用微信支付)
- 广州4a广告公司(广州4a广告公司创意总监年薪)
- 抖音卡(抖音卡顿怎么解决)
- xboxones(xboxone手柄怎么配对主机)
- 兵马俑(兵马俑介绍和历史背景)
- 陈武简历
- 帆船比赛(帆船比赛视频)
- 韩国媳妇和小雪(韩国媳妇和小雪的父亲工资是多少)
- 儋州市第二中学(儋州市第二中学录取分数线)
- 鬼泣5攻略(鬼泣5攻略第三关怎么跳)
- 地球日主题(2020年世界地球日主题)
- 和柳亚子(和柳亚子先生于田)
- 冰客(冰客果汁)
- yy魔兽(yy魔兽世界)
- 国外成人游戏(国外成人游戏注册需要visa信用卡)
- 郭妮小说(恶魔的法则郭妮小说)
- 东天目山(东天目山景区)
- 杭同(杭同培训中心怎么样)
- 蝙蝠给人类的一封信(蝙蝠给人类的一封信)
- 大松电饭煲(美的大松电饭煲)
- 服饰加盟(服饰加盟店招商)
- 疯狂填字(疯狂填字5)
- 点对点短信息(点对点短信息费是什么意思)
- 观音普门品(观音普门品念诵全文)
- 河北省大运会(河北省大运会时间)
- 哈利波特官网(哈利波特官网在哪里)
- 广州晓港公园(广州晓港公园正门图片)
- 钢筋等级符号(钢筋等级符号电脑怎么输入)
- 常州天宁寺(常州天宁寺求什么灵验)
