我试图通过我的例子找出带有界限的多键索引。
s1:PRIMARY> db.inventory.find();
{ "_id" : 2, "item" : "ABCx", "ratings" : [ 4, 3 ] }
{ "_id" : 3, "item" : "ABC", "ratings" : [ 2, 9 ] }
{ "_id" : 4, "item" : "XYZ", "ratings" : [ 4, 3 ] }
我做了这样的发现
s1:PRIMARY> db.inventory.find( { ratings: { $gte: 5 ,$lte:8} } );
输出:
{ "_id" : 3, "item" : "ABC", "ratings" : [ 2, 9 ] }
根据此处的文档和我引用(引用包含与我不同的示例)
查询在评级数组中搜索至少一个更大的元素 小于或等于3且至少一个元素小于或等于6。 因为单个元素不需要满足两个标准,MongoDB 不与边界相交并使用[[3,Infinity]]或[ [-Infinity,6]]。 MongoDB不保证这些中的哪一个 它选择的两个界限。
https://docs.mongodb.com/manual/core/multikey-index-bounds/
基于此文档,我应该将所有3行作为输出而不是仅仅一行。
有人可以澄清一下这是如何运作的?
答案 0 :(得分:1)
// HYPOTHESIS:
// We believe that {{ criteria || '[criteria]' }} of {{ (customer != null) ? customer : '[customer]' }} are most frustrated about {{ (task != null) ? task : '[task]' }} because {{ problem || '[problem]' }}.
// CUSTOMER SCREENING CRITERIA:
// We are looking for {{ (customer != null) ? customer : '[customer]' }} who are regularly involved in {{ (task != null) ? task : '[task]' }}.
// CUSTOMER INTERVIEW QUESTIONS:
// Tell me about the last time when you were {{ (task != null) ? task : '[task]' }}?
// What are the major challenges with {{ (task != null) ? task : '[task]' }}?
// On a scale of 0-10, how frustrating are these problems? Why?
// If you could wave a magic wand and be able to do anything about {{ (task != null) ? task : '[task]' }} in your daily job, what would it be?
和$gte
必须与$lte
方法中文档的result
数组中的至少一个元素匹配。所以,对于.find()
:
$gte: 5, $lte: 8
有_id: 2
=> [ 4, 3 ]
匹配任一元素,但$lte: 8
无法与$gte: 5
或3
匹配,因此不会返回; 4
有_id: 3
=> [ 2, 9 ]
匹配$lte: 8
和2
匹配$gte: 5
,因此会返回; 9
有_id: 4
=>与[ 4, 3 ]
相同。因此,只返回带有_id: 2
的文档才有意义。添加_id: 3
到$gte: 5
或_id: 2
文档的_id: 4
数组的元素,它将匹配该文档!