Second parameter of db.col.createIndex

Could someone explain to me exactly what this index means or implies?

db.x.createIndex( { “user.login”: 1, “user.date”: -1 }, “myIndex” )

The second parameter “myIndex” catches my attention and I don’t understand where to find this kind of thing in the mongo guide. Any reference?

Thank you!

The first parameter is specifying the shape of the compound index. First, by sorting embedded field user.login in ascending (1) order, and then by sorting user.date in descending (-1) order within the context of user.login. The field order and the direction ( 1 or -1) of sort is important in a compound index.

The second parameter to the createIndex function (https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#options-for-all-index-types) is the name of the index.

Thanks,
Mahi

1 Like

Thank you very much, I really couldn’t find the logic, I already read “options for all index types” → name