MongoDB Explain()中IndexBounds字段解析及两类查询场景疑问解答
2. Breaking Down the users Collection's Index Bounds: ["[\"HARRIS\", \"HARRIT\")", "[/^HARRIS(.*)/, /^HARRIS(.*)/]"]
Your regex query LastName:{$regex:/^HARRIS(.*)/} is a prefix match (starts with "HARRIS"), which MongoDB can optimize using an index on LastName. Here’s what each part means:
Part 1: ["HARRIS", "HARRIT")
This is a lexicographical range scan that takes advantage of the index’s sorted order:
- The lower bound
"HARRIS"is the exact prefix you’re targeting. - The upper bound
"HARRIT"is calculated by incrementing the last character of your prefix ("HARRIS" ends with 'S', so we bump it to 'T' to get "HARRIT"). - Any string starting with "HARRIS" will sit between these two values in lex order (the
)means the upper bound is exclusive—so "HARRIT" itself isn’t included). This lets MongoDB quickly skip all index entries that can’t possibly match your regex, saving time.
Part 2: [/^HARRIS(.*)/, /^HARRIS(.*)/]
This is a regex-specific filter that runs after the range scan:
- Even though the range scan captures all strings starting with "HARRIS", MongoDB uses your original regex to double-check that each document in the range actually matches the pattern.
- The
[regex, regex]format tells MongoDB to match exactly the regex you provided for theLastNamefield. In this case, since/^HARRIS(.*)/is the same as/^HARRIS/, this is mostly a safeguard to ensure no edge cases slip through the range scan.
内容的提问来源于stack exchange,提问作者Prince Chisomaga
相关产品推荐
相关产品推荐

