Create TTL index fail . The expireAfterSeconds have limit value?

mongo version - 4.0

When I create the TTL index throws out ( [js] SyntaxError: illegal character) error.

below command fails to create the TTL.

db.eventLog.createIndex({"updatedDate": 1}, {expireAfterSeconds:15552000<U+202C> } )  
db.eventLog.createIndex({"updatedDate":1}, {expireAfterSeconds: 15552000<U+202C>,background:true})   
db.eventLog.createIndex({ "updatedDate" : 1}, {background:true,"name" : "updatedDate",expireAfterSeconds: NumberLong(15552000<U+202C>)})   

but when I change the expireAfterSeconds value from 15552000‬ to 3888000 create TTL success.

or change to those command create TTL success, but show the index seems very strange.

db.eventLog.createIndex({ "updatedDate" : 1}, {background:true,"name" : "updatedDate",expireAfterSeconds:  { $numberLong: "15552000<U+202C>" }})   
        {
                "v" : 2,
                "key" : {
                        "updatedDate" : 1
                },
                "name" : "updatedDate_1",
                "ns" : "bccd_asset_connector.eventLog",
                "expireAfterSeconds" : { $numberLong: "15552000<U+202C>" },
                "background" : true
        }
db.eventLog.createIndex({"updatedDate": 1}, { "expireAfterSeconds": "15552000<U+202C>", background: true })  
        {
                "v" : 2,
                "key" : {
                        "updatedDate" : 1
                },
                "name" : "updatedDate_1",
                "ns" : "bccd_asset_connector.eventLog",
                "expireAfterSeconds" : "15552000<U+202C>",
                "background" : true
        }

Welcome to the MongoDB Community @harz_wang !

I noticed your output includes an extra unprintable Unicode character (U+202C) after the expireAfterSeconds value, which seems likely to be the cause of the illegal character error you are encountering in the MongoDB shell.

Try creating the index without any extra characters:

db.eventLog.createIndex({"updatedDate": 1}, {expireAfterSeconds: 15552000} )  

Regards,
Stennie

1 Like

Hello Stennie,
Thank you for your answer.

I tried to re-enter the command manually and it worked. Your are right. Thanks.

May I ask one more question?
Why can the expireAfterSeconds create success by those formats (JSON or String), means the expireAfterSeconds does not value type check, or higher version will have to check the value type?

expireAfterSeconds:  { $numberLong: "15552000" }
expireAfterSeconds: "15552000"

Thank you very much! Have a nice day. :+1:

Hi @harz_wang,

Modern versions of MongoDB server validate the type for expireAfterSeconds which should be a positive integer.

For example, in 4.2.23:

> db.eventLog.createIndex({"updatedDate": 1}, {expireAfterSeconds: -15552000} )
{
	"ok" : 0,
	"errmsg" : "TTL index 'expireAfterSeconds' option cannot be less than 0. Index spec: { key: { updatedDate: 1.0 }, name: \"updatedDate_1\", expireAfterSeconds: -15552000.0 }",
	"code" : 67,
	"codeName" : "CannotCreateIndex"
}
> db.eventLog.createIndex({"updatedDate": 1}, {expireAfterSeconds: "15552000"} )
{
	"ok" : 0,
	"errmsg" : "TTL index 'expireAfterSeconds' option must be numeric, but received a type of 'string'. Index spec: { key: { updatedDate: 1.0 }, name: \"updatedDate_1\", expireAfterSeconds: \"15552000\" }",
	"code" : 67,
	"codeName" : "CannotCreateIndex"
}
> db.eventLog.createIndex({"updatedDate": 1}, {expireAfterSeconds: { $numberLong: "15552000" } } )
{
	"ok" : 0,
	"errmsg" : "TTL index 'expireAfterSeconds' option must be numeric, but received a type of 'object'. Index spec: { key: { updatedDate: 1.0 }, name: \"updatedDate_1\", expireAfterSeconds: { $numberLong: \"15552000\" } }",
	"code" : 67,
	"codeName" : "CannotCreateIndex"
}

If you are using a version of MongoDB server older than 4.2 (which is currently the oldest release series that hasn’t reached End of Life yet), I recommend planning to upgrade. There are many bug fixes and improvements, including better validation and error messages.

Regards,
Stennie

1 Like

Hi Stennie
Thank you & have a nice day. :+1:

Best Regards
Harz

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.