0

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

Pierre Clocher
  • 1,067
  • 1
  • 9
  • 21
  • "Is it a bad practice to update _id as following" - yeah, it's so much a bad practice that it's forbidden by the database. – Sergio Tulentsev Jul 16 '18 at 10:23
  • @SergioTulentsev I feel quite ashamed because I tried it before posting my question but with the same value ^^ (`db.myCollection.update({ _id: ObjectId("5a58c8c9aada3a7c382cfb2f") }, { $set: { _id: ObjectId("5a58c8c9aada3a7c382cfb2f") } })`) indeed with a different value I get the error. – Pierre Clocher Jul 16 '18 at 10:30

0 Answers0