3

For example, this is a record:

 { 
     "_id" : ObjectId("576bc7a48114a14b47920d60"), 
     "id" : "TEST0001", 
     "testTime" : ISODate("2016-06-23T11:28:06.529+0000")
 }

The testTime is ISODate, does Mongodb query the record by testTime is quicker than this? :

{ 
     "_id" : ObjectId("576bc7a48114a14b47920d60"), 
     "id" : "TEST0001", 
     "testTime" : "2016-06-23 11:28:06"
 }
profesor79
  • 9,213
  • 3
  • 31
  • 52
adairjun
  • 325
  • 4
  • 16

1 Answers1

4

yes it does.

The difference is produced on basis that date object is stored as a number in dateTime object.

To understood this we could go with this ilustration:

When there is a query on dateTime filed and dateTime is stored in numerical object, that means we have comparison on numbers. Mongo will compare object with size of 64 bits (8bytes) see here with same object.

When comparing string, mongo loads string like this: 2016-06-27T08:39:44.000 which is 23 chars*2bytes (utf) => 46 bytes to compare in memory and need to check all bytes from highest to lowest one..

Now, you know the answer why it is faster using dateObject instead of string.

Any comments welcome!

link here

Comparison/Sort Order

  1. MinKey (internal type)
  2. Null
  3. Numbers (ints, longs, doubles)
  4. Symbol, String
  5. Object
  6. Array
  7. BinData
  8. ObjectId
  9. Boolean
  10. Date
  11. Timestamp
  12. Regular
  13. Expression
  14. MaxKey (internal type)
profesor79
  • 9,213
  • 3
  • 31
  • 52