1

Does MongoDB createIndexes() method, support specifying individual options for each index set in the script. Something like this below, which is not working by the way.

Ref - https://www.mongodb.com/docs/v5.0/reference/method/db.collection.createIndexes/#mongodb-method-db.collection.createIndexes

var indexes = [
  {
    key: { "productId":1, "productName":1 },
    partialFilterExpression: { "catalog": { $exists: true} },
    name:"productId_productName_catalog_partial"
  },
  {
    key: { "productId":1, "createdDate":1 },
    sparse: true,
    name:"productId_createdDate_only"
  }
  
];
db.product.createIndexes(indexes);

Error

{
    "message" : "Error in specification { key: { key: { productId: 1, productName: 1 }, partialFilterExpression: { catalog: { $exists: true } }, name: 'productId_productName_catalog_partial' }, name: 'key_[object Object]_partialFilterExpression_[object Object]_name_productId_productName_catalog_partial' } :: caused by :: Unknown index plugin 'productId_productName_catalog_partial'",
    "ok" : 0,
    "code" : 67,
    "codeName" : "CannotCreateIndex"
}
CSK 4ever
  • 45
  • 4

2 Answers2

1

Looks like the below way of execution works fine with multiple index set with different option specification.

"name" field is a mandatory one which also makes it to work.

db.runCommand({
    createIndexes: "product",
    indexes: [
        {
            key: { "productId": 1, "productName": 1 },
            partialFilterExpression: { "catalog": { $exists: true } },
            name: "productId_productName_catalog_partial"
        },
        {
            key: { "productId": 1, "createdDate": 1 },
            sparse: true,
            name: "productId_createdDate_only"
        }
    ]

});

Output

{
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 3,
    "createdCollectionAutomatically" : false,
    "ok" : 1
}
CSK 4ever
  • 45
  • 4
0

Update

While what you describe does seem to be a limitation of the helper/wrapper function, it does not seem to be a limitation of the underlying createIndexes command itself.

Here is an example that creates three indexes each with different options/properties:

> db.foo.getIndexes()
[ 
  { v: 2, key: { _id: 1 }, name: '_id_' } 
]
rS [direct: primary] test> db.foo.runCommand({
  createIndexes: 'foo',
  indexes: [
    { key: { x: 1 }, name: 'x_1', unique: true },
    { key: { y: 1 }, name: 'y_1', sparse: true },
    {
      key: { z: 1 },
      name: 'z_1',
      partialFilterExpression: { z: { exists: true } }
    }
  ]
});
{
  numIndexesBefore: 1,
  numIndexesAfter: 4,
  ok: 1,
  ...
}
rS [direct: primary] test> db.foo.getIndexes()
[
  { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2, key: { x: 1 }, name: 'x_1', unique: true },
  { v: 2, key: { y: 1 }, name: 'y_1', sparse: true },
  {
    v: 2,
    key: { z: 1 },
    name: 'z_1',
    partialFilterExpression: { z: { exists: true } }
  }
]

Original answer regarding the helper/wrapper function below.


The Options section of the documentation you linked has this to say on the matter:

IMPORTANT

When you specify options to db.collection.createIndexes(), the options apply to all of the specified indexes. For example, if you specify a collation option, all of the created indexes will include that collation.

db.collection.createIndexes() will return an error if you attempt to create indexes with incompatible options or too many arguments. Refer to the option descriptions for more information.

That, and the outcome of your testing, suggests that the answer to your question is: No, specifying options for the indexes individually is not supported with this command.

user20042973
  • 4,096
  • 2
  • 3
  • 14
  • Well, I hope MongoDB provides this feature in future, as this would also help in index maintenance and versioning in case of reindex situation. – CSK 4ever Jun 24 '23 at 04:39
  • Check the updated answer - looks like this _is_ possible using the underlying command itself. – user20042973 Jun 24 '23 at 17:00
  • 1
    I have posted an answer, please check it out, upon trying the way you have mentioned, it works fine. – CSK 4ever Jun 26 '23 at 04:42