[SERVER-27528] partialFilterCondition index ignores $exists:true condition Created: 27/Dec/16 Updated: 03/Jan/17 Resolved: 03/Jan/17 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Querying |
| Affects Version/s: | 3.2.10, 3.4.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Kamal Kalra | Assignee: | Kelsey Schubert |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Operating System: | ALL |
| Participants: |
| Description |
|
I have a collection with a partial index created using the following command:
As you can see, I'm trying to set up a unique partial constraint anytime the field "spaceOwner" exists. The combination of spaceOwner / key is always unique anytime the field spaceOwner exists. This is guaranteed by the application. However, I keep getting duplicate key exceptions for when the spaceOwner field doesn't exist which makes sense because in that scenario, there will indeed be duplicates but the index should only check when the spaceOwner key exists, which it doesn't seem to be doing. Here is an example stack trace:
|
| Comments |
| Comment by Kamal Kalra [ 03/Jan/17 ] | |||||||||||||
|
Ah!!! Thank you. Can't believe I missed that. | |||||||||||||
| Comment by James Wahlin [ 03/Jan/17 ] | |||||||||||||
|
Hi Kamal, Partial index filters are supported in MongoDB 3.2. 'partialIndexCondition' was a typo on my part. 'partialFilterCondition' is also not a valid option for index creation. What you should be using is 'partialFilterExpression'. Given that your statement becomes:
Thanks, | |||||||||||||
| Comment by Kamal Kalra [ 03/Jan/17 ] | |||||||||||||
|
My query states "partialFilterCondition", not "partialIndexCondition". I've followed the guidelines in the documentation and I don't see what's wrong with the query expression. Ex):
| |||||||||||||
| Comment by James Wahlin [ 03/Jan/17 ] | |||||||||||||
|
Use of 'partialIndexCondition' as an index option has never been supported. Under MongoDB 3.2 you can specify this option but it will be ignored, which we recognize is not ideal behavior and explains the behavior you are seeing. To help address cases like this, where a wrong keyword is used or a keyword is mis-spelled, we added stricter validation for index creation under If you are still encountering issues creating or using this index, I would suggest reading our Partial Indexes documentation which details this feature. Best, | |||||||||||||
| Comment by Kamal Kalra [ 03/Jan/17 ] | |||||||||||||
|
This is on MongoDB 3.2.10. I incorrectly listed 3.4.1 in the description (wasn't able to edit it after the issue was submitted). But the syntax seems correct. Mongo (3.2.10) doesn't complain about an invalid index, etc.. | |||||||||||||
| Comment by Kelsey Schubert [ 03/Jan/17 ] | |||||||||||||
|
Hi kamalkalra, Thanks for clarifying. I took a second look and it appears that you are specifying the partial index expression incorrectly. Using the command you provided in MongoDB 3.4.1 results in the following error message:
Please use partialFilterExpression as documented here. Thank you, | |||||||||||||
| Comment by Kamal Kalra [ 03/Jan/17 ] | |||||||||||||
|
The field was omitted. The stack trace display null even when the field doesn't exist (which is another issue by itself). Execute this simple test in the shell and it'll throw an error:
| |||||||||||||
| Comment by Kelsey Schubert [ 03/Jan/17 ] | |||||||||||||
|
Hi kamalkalra, This behavior is expected when MongoDB is sent a document with the literal null in spaceOwner field. Since the field exists in the document, MongoDB attempts to index the null value. If the field were instead omitted, you should not see duplicate key exceptions. Kind regards, |