Do we have mechanism to keep indexes on some time range documents means put index on last one year documents of collection.
I know TTL indexes but TTL indexes also delete the document form collection which is not my requirement. I do not want to delete those documents from collection.
Kindly suggest.
Hi,
Do we have mechanism to keep indexes on some time range documents means put index on last one year documents of collection.
I know TTL indexes but TTL indexes also delete the document form collection which is not my requirement. I do not want to delete those documents from collection.
If I understand correctly, you are trying to remove some documents from the index after a period of time, presumably to put a limit on index size. Is this correct?
Currently, TTL index is the only time-related indexes available in MongoDB. However, you can probably achieve a similar effect using a sparse index. A sparse index will only create an index entry for documents having a certain field. Documents without that field will not be included in the index. To illustrate:
> // Insert 3 documents into a collection:
> db.test.insert([ {a:1,b:1}, {a:2,b:2}, {a:3,b:3} ])
> // Create a sparse index on field 'a'
> db.test.createIndex({a:1},{sparse:true})
> // Find documents using the index (by using hint() to instruct MongoDB to use the sparse index)
> db.test.find().hint({a:1})
{ "_id" : ObjectId("5817e171fc6ddacd354e510f"), "a" : 1, "b" : 1 }
{ "_id" : ObjectId("5817e171fc6ddacd354e5110"), "a" : 2, "b" : 2 }
{ "_id" : ObjectId("5817e171fc6ddacd354e5111"), "a" : 3, "b" : 3 }
> // Unset the 'a' field on one of the documents
> db.test.update({a:1},{$unset:{a:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> // The document is not present in the index anymore
> db.test.find().hint({a:1})
{ "_id" : ObjectId("5817e171fc6ddacd354e5110"), "a" : 2, "b" : 2 }
{ "_id" : ObjectId("5817e171fc6ddacd354e5111"), "a" : 3, "b" : 3 }
> // However it still exists in the collection
> db.test.find()
{ "_id" : ObjectId("5817e171fc6ddacd354e510f"), "b" : 1 }
{ "_id" : ObjectId("5817e171fc6ddacd354e5110"), "a" : 2, "b" : 2 }
{ "_id" : ObjectId("5817e171fc6ddacd354e5111"), "a" : 3, "b" : 3 }
If applicable, you can use a scheduled task to remove the indexed fields from some documents. Those documents will subsequently be removed from the sparse index as well.
For more information, please see:
Best regards,
Kevin
Hi,
Do we have mechanism to keep indexes on some time range documents means put index on last one year documents of collection.
I know TTL indexes but TTL indexes also delete the document form collection which is not my requirement. I do not want to delete those documents from collection.
Sorry I forgot to mention in my post earlier about the Partial Indexes which is a new feature in MongoDB 3.2. Essentially, partial indexes is a more powerful variant of the aforementioned sparse indexes by allowing you to provide an expression that will act as a filter to include/not include a certain document into the index. See Comparison with the sparse index for more details.
Also see Create a Partial Index for a full list of available filter expressions, and to discover if your requirement is best served with a sparse or a partial index.
Best regards,
Kevin