[DOCS-12534] [Server] createIndex -- a single index may be both Partial and TTL Created: 08/Mar/19  Updated: 13/Nov/23  Resolved: 10/Nov/23

Status: Closed
Project: Documentation
Component/s: Server
Affects Version/s: None
Fix Version/s: Server_Docs_20231113

Type: Task Priority: Critical - P2
Reporter: Mark Brinsmead Assignee: Ian Fogelman
Resolution: Fixed Votes: 4
Labels: proactive, server-docs-bug-bash
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 2 years, 31 weeks, 1 day ago

 Description   

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.

 



 Comments   
Comment by Andre Spiegel [ 06/Jul/21 ]

Let me add that I only learned about this possibility today, when discussing possible enhancements to TTL indexes to allow more complex conditions. So this definitely needs to be validated (whether it's really supposed to work), and clearly documented in the sections on both TTL indexes and partial indexes.

Generated at Thu Feb 08 08:05:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.