gpt4 book ai didi

$elemMatch 的 MongoDB 索引

转载 作者:IT老高 更新时间:2023-10-28 13:15:01 24 4
gpt4 key购买 nike

索引帮助页面位于 http://www.mongodb.org/display/DOCS/Indexes没有提到 $elemMatch 并且因为它说要在我的 2M+ 对象集合上添加一个索引,所以我想我会问这个:

我正在做如下查询:

{ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }

如果我添加一个索引

{lc:1, group:1, indices.text:1, indices.pos:1}

这个带有 $elemMatch 组件的查询是否能够完全通过索引运行?

最佳答案

根据您的查询,我认为您的文档如下所示:

{
"_id" : 1,
"lc" : "eng",
"group" : "xyz",
"indices" : [
{
"text" : "as",
"pos" : 2
},
{
"text" : "text",
"pos" : 4
}
]
}

我使用这种格式的文档创建了一个测试集合,创建了索引,并运行了您使用 .explain() 选项发布的查询。

索引正在按预期使用:

> db.test.ensureIndex({"lc":1, "group":1, "indices.text":1, "indices.pos":1})
> db.test.find({ lc: "eng", group: "xyz", indices: { $elemMatch: { text: "as", pos: { $gt: 1 } } } }).explain()
{
"cursor" : "BtreeCursor lc_1_group_1_indices.text_1_indices.pos_1",
"isMultiKey" : true,
"n" : NumberLong(1),
"nscannedObjects" : NumberLong(1),
"nscanned" : NumberLong(1),
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : NumberLong(0),
"millis" : 0,
"indexBounds" : {
"lc" : [
[
"eng",
"eng"
]
],
"group" : [
[
"xyz",
"xyz"
]
],
"indices.text" : [
[
"as",
"as"
]
],
"indices.pos" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
},
"server" : "Marcs-MacBook-Pro.local:27017"
}

.explain() 特性的文档可以在这里找到:http://www.mongodb.org/display/DOCS/Explain

.explain() 可用于显示有关查询的信息,包括使用了哪个(如果有)索引。

关于$elemMatch 的 MongoDB 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com