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.
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"
}
2
Answers
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.
Output
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:
Original answer regarding the helper/wrapper function below.
The Options section of the documentation you linked has this to say on the matter:
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.