Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-12534

[Server] createIndex -- a single index may be both Partial and TTL

      Description

      It appears that – at least on 4.0.5 – indexes can be both Partial and TTL

      The documentation seems rather silent on this point.  Perhaps it is not meant to work?

      Example

      The documentation is not entirely clear on the subject of creating indexes that are *both "partial" and "TTL", so I have tested this myself in a version 4.0.5 mongod. I found that I could indeed create an index that combined the attributes of "TTL Index" and "Partial Index", and that it did indeed expire only documents matching the partialFilterExpression defined on the index.

      As an example, on the collection test.foo, I created an index:

      db.foo.createIndex({F:1}, {partialFilterExpression: {D: 1}, expireAfterSeconds: 10})

      {{}}

      and then inserted two documents into the collection:
      {{}}

      { "_id" : ObjectId("5c8188656c5c4825af60c90e"), "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3 }
       { "_id" : ObjectId("5c81886c6c5c4825af60c90f"), "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 1 }

      {{}}

      Here, the index will expire documents when the F field contains a Date more than 10 seconds old, but we intend it to expire only those documents where D = 1 – the document with D = 3 should be left untouched.

      Waiting for about a minute for the index to activate, this is the data left in the collection:
      {{}}

      { "_id" : ObjectId("5c8188656c5c4825af60c90e"), "F" : ISODate("2019-03-07T20:59:18.428Z"), "D" : 3 }

      {{}}

      The index has expired the data we wanted expired, and only that data

      Scope of changes

      • ToDo: Explore application of partial property on other index properties or types
      • Update documentation for partial indexes to explicitly state it's applicability to constraining other index properties or types.

       

            Assignee:
            ian.fogelman@mongodb.com Ian Fogelman
            Reporter:
            mark.brinsmead@mongodb.com Mark Brinsmead
            Votes:
            4 Vote for this issue
            Watchers:
            8 Start watching this issue

              Created:
              Updated:
              Resolved:
              2 years, 42 weeks, 3 days ago