简化的例子和问题
为了尽可能地简单,我将跳到一个例子:
具有多键索引的foo集合{searchTags: 1}
{_id: 1, searchTags: [{bar: "BAR"}]}
{_id: 2, searchTags: [{baz: "BAZ"}]}我正在尝试使用baz字段键获取所有嵌入式文档(无需使用$exists --稍后我可以尝试解释原因)。为什么
{searchTags: {$elemMatch: { $gte: { baz: MinKey() }, $lte: { baz: MaxKey() }}}}
返回两个文档(不首选),但是
{searchTags: {$elemMatch: { $gte: { baz: "" }, $lte: { baz: MaxKey() }}}}
只返回{_id: 2, searchTags: [{baz: "BAZ"}]} (首选)?
Sidenote:快速的模式细节,以避免“您在做什么?”问题
foo文档中的其他字段名,值是它们的字段值。我可以对每个字段进行索引,但是它们是部分索引,而且会有很多索引。为了灵活起见,将它们合并到一个具有多键索引的数组中是最有意义的。发布于 2020-09-30 11:03:55
{searchTags: 1}上的索引不用于服务该查询。索引数组时,数组的每个元素都作为值包含在索引中。任何碰巧是文档的元素都是作为整个文档的索引,它不是按字段细分的。我希望如果您使用explain运行该命令,它将显示它是一个集合扫描。
快速演示:
> db.collection.insertMany([
{_id: 1, searchTags: [{bar: "BAR"}]},
{_id: 2, searchTags: [{baz: "BAZ"}]}
])
{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }
> db.collection.aggregate([
{$unwind: "$searchTags"},
{$addFields: {
baztype: {$type: "$searchTags.baz"},
bazmin: {$gt: ["$searchTags.baz", MinKey()]},
bazmax: {$lt: ["$searchTages.baz", MaxKey()]},
bazstr: {$gt: ["$searchTags.baz", ""]}
}}
])
{ "_id" : 1, "searchTags" : { "bar" : "BAR" }, "baztype" : "missing", "bazmin" : true, "bazmax" : true, "bazstr" : false }
{ "_id" : 2, "searchTags" : { "baz" : "BAZ" }, "baztype" : "string", "bazmin" : true, "bazmax" : true, "bazstr" : true }显示的第一个查询匹配,因为每个可能的值,包括未定义的值,都大于MinKey,小于MaxKey。
您显示的第二个查询不匹配,因为运算符区分大小写,所以当提供字符串值时,只有字符串值匹配,这不包括未定义的字符串值。
db.collection.find({"searchTags.baz":{$exists:true}})https://stackoverflow.com/questions/64133279
复制相似问题