1

I'm trying to update a TTL collection with the PyMongo. Trying to run this I get 'failed no such cmd: index'

client.db.command({'collMod': url,
                    'index': {'keyPattern': {'dateCreated':1},
                           'expireAfterSeconds': 3600}})

anyone shine some light on what I'm doing wrong?

ThrowsException
  • 2,586
  • 20
  • 37

2 Answers2

5

I believe that this would work assuming that url contains the name of the collection with the index you are modifying:

client.db.command('collMod', url,
                  index={'keyPattern': {'dateCreated':1},
                         'expireAfterSeconds': 3600}})

For anyone else looking for a solution to this I managed with the following:

client.db.command('collMod', 'notifications', 
                  index={'keyPattern': {'expr': 1}, 
                         'background': True, 
                         'expireAfterSeconds': 604800})

Which results in the following output:

{u'expireAfterSeconds_old': 3888000, 
 u'expireAfterSeconds_new': 604800, u'ok': 1.0}
Rob
  • 3,687
  • 2
  • 32
  • 40
0

Well I've just decided to switch my index around to instead set the time I want the document to expire at in the database instead of saying how long I want the document to live by using

db.test.ensure_index("expireOn", expireAfterSeconds=0)

Now I can set the field to a future time that I want the entry to expire instead of having to change the index. Seems strange that as of now there is no way to update the index programatically using python.

ThrowsException
  • 2,586
  • 20
  • 37