Considering a MongoDB Document as following
{
"_id": ObjectId("59f05fd6aa6e2bdef4275ebb"),
"property": "example value",
"updatedAt": ISODate("2018-07-14T01:00:00+01:00"),
"createdAt": ISODate("2018-07-15T01:00:00+01:00"),
"deletedAt": ISODate("2018-07-16T01:00:00+01:00")
}
My questions are about MongoDB best practices and especially minimising response time and indexing costs.
Since creation timestamp is encoded in _id and queryable with comparison operators ($gt(e)
, $lt(e)
...) (see this post) : what's the most performant between directly querying "_id" property and consequently keeping _id the only collection's index and adding ISODate field createdAt
and also indexing this field ?
Finally, how to choose between indexing a single property (deletedAt
) or combining them (_id
, deletedAt
) knowing that the most frequent query will be something like that
db.myCollection.find({
deletedAt: { $exists: false },
_id: { $gt: ObjectId('somepastobjectid') }
})
Note that _id is indexed by default and I just need to check existence of deletedAt
(the value will be used very occasionally)
Thank you for your answers