Skip to content

**Describe the bug** #1932

@OneClick1-MF

Description

@OneClick1-MF

Describe the bug

The CreateIndex command fails with an error when the $in operator is used within the partialFilterExpression clause. The failure occurs specifically when attempting to create an index with a partial filter expression that includes a field with $in: [ "" ]. Replacing $in with $eq for the same field succeeds as expected.

Reproduction Steps

Run the following command (with all identifiers obfuscated for privacy):

// Fails
collectionRef.createIndex({
  propA: 1,
  propB: 1,
  propC: 1,
  propD: 1,
  propE: 1,
  propF: 1,
  'propG.propH': 1
}, {
  name: 'index_alpha_xyz',
  partialFilterExpression: {
    propA: 'TRUE',
    propB: 'CC0',
    propC: { '$in': [ '' ] },
    propD: [],
    propE: 'SOME_TYPE',
    propF: { '$exists': true }
  }
})

Output:

MongoServerError[CannotCreateIndex]: Error in specification { "name": "index_alpha_xyz", "partialFilterExpression": { "propA": "TRUE", "propB": "CC0", "propC": { "$in": [ "" ] }, "propD": [  ], "propE": "SOME_TYPE", "propF": { "$exists": true } }, "key": { "propA": 1, "propB": 1, "propC": 1, "propD": 1, "propE": 1, "propF": 1, "propG.propH": 1 } }:Expression not supported in partial index: propC $in [ "" ]

Running the same command with $eq works:

// Success
collectionRef.createIndex({
  propA: 1,
  propB: 1,
  propC: 1,
  propD: 1,
  propE: 1,
  propF: 1,
  'propG.propH': 1
}, {
  name: 'index_alpha_xyz',
  partialFilterExpression: {
    propA: 'TRUE',
    propB: 'CC0',
    propC: '',
    propD: [],
    propE: 'SOME_TYPE',
    propF: { '$exists': true }
  }
})

collectionRef.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  {
    v: 2,
    key: {
      propA: 1,
      propB: 1,
      propC: 1,
      propD: 1,
      propE: 1,
      propF: 1,
      'propG.propH': 1
    },
    name: 'index_alpha_xyz',
    partialFilterExpression: { propA: 'TRUE', propB: 'CC0', propC: '' }
  }
]

Expected behavior

The $in operator in partialFilterExpression should be accepted and work as expected, similar to $eq. If $in: [ '' ] is not supported, this should be explicitly documented with an appropriate error message upfront.

Actual behavior

Attempting to use $in: [ "" ] for a property in partialFilterExpression results in an error, while $eq succeeds for the same property and value.

Additional context

The issue was discovered when migrating filter index logic from an application. Explicit handling for $in would allow a broader range of filter expressions and assist in migration scenarios.

Originally posted by @sivethe in documentdb/documentdb#434

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions