6

For example, I created this index:

db.test.createIndex({field1:1}, {partialFilterExpression:{field2:{$gt:10}}})

Index created successfully. Then I want to create second index:

db.test.createIndex({field1:1}, {partialFilterExpression:{field2:{$lte:10}}}

But above will give error IndexOptionsConflict, but no error details, only dumps the index object.

I have tested to create such indexes on PostgreSQL and it works. How can I do this in MongoDB?

1
  • You cannot create an index with the same field but different options on MongoDB. See SERVER-25023 for possible workarounds.
    – kevinadi
    Nov 13, 2019 at 0:17

2 Answers 2

0

It's worth noting that this will most likely be fixed with 4.8 stable release. https://jira.mongodb.org/browse/SERVER-25023

1
  • It's already been fixed in MongoDB 6.0, I checked it.
    – Elect2
    Feb 21, 2023 at 17:25
3

Sadly, a restriction of MongoDB:

Restrictions

In MongoDB, you cannot create multiple versions of an index that differ only in the options. As such, you cannot create multiple partial indexes that differ only by the filter expression.

Right now, you can only do this:

db.test.createIndex({field1:1}, {partialFilterExpression:{field2:{$gt:10,$lte:10}}})

Or use composite indexes with a placeholder field:

db.test.createIndex({field1:1, never_use_this_field1: 1}, {partialFilterExpression:{field2:{$gt:10}}})
db.test.createIndex({field1:1, never_use_this_field2: 1}, {partialFilterExpression:{field2:{$lte:10}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.