[SERVER-28058] Able to create TTL index on single key which is previously indexed Created: 20/Feb/17  Updated: 21/Feb/17  Resolved: 21/Feb/17

Status: Closed
Project: Core Server
Component/s: Index Maintenance, TTL
Affects Version/s: 3.4.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Nikhil Kumar Gupta Assignee: Mark Agarunov
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

1) Created the single field index.
2) Create a TTL index on the same Key field

Participants:

 Description   

As per the documentation for 3.4, if a non-TTL single-field index already exists for a field, you cannot create a TTL index on the same field since you cannot create indexes that have the same key specification and differ only by the options. To change a non-TTL single-field index to a TTL index, you must drop the index first and recreate with the expireAfterSeconds option.

        {
                "v" : 2,
                "key" : {
                        "date" : 1
                },
                "name" : "date_1",
                "ns" : "training.toys"
        },
        {
                "v" : 2,
                "key" : {
                        "date" : 1,
                        "expireAfterSeconds" : 3900
                },
                "name" : "date_1_expireAfterSeconds_3900",
                "ns" : "training.toys"
        }



 Comments   
Comment by Mark Agarunov [ 21/Feb/17 ]

Hello compchap_nikhil,

Thank you for the report. Looking over the output provided, it appears that the index was created with expireAfterSeconds as part of the key instead of being passed as an option. In the output you've provided, expiresAfterSeconds appears to be under key, however when passing it as an option, it would be on the same level as the key object:

(mongod-3.4.2) test> db.test.createIndex({"3":1}, {expireAfterSeconds: 3600,name: "test3"})
{
  "createdCollectionAutomatically": false,
  "numIndexesBefore": 3,
  "numIndexesAfter": 4,
  "ok": 1
}
(mongod-3.4.2) test> db.test.getIndexes()
[
  {
    "v": 2,
    "key": {
      "3": 1
    },
    "name": "test3",
    "ns": "test.test",
    "expireAfterSeconds": 3600
  }
]

Please note that SERVER project is for reporting bugs or feature suggestions for the MongoDB server. For MongoDB-related support discussion please post on the mongodb-user group or Stack Overflow with the mongodb tag. A question like this involving more discussion would be best posted on the mongodb-user group.

Thanks,
Mark

Generated at Thu Feb 08 04:17:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.