Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Mongodb queries a record by DateTime quicker than by String?

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"
 }
like image 719
adairjun Avatar asked Mar 18 '26 03:03

adairjun


1 Answers

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)
like image 183
profesor79 Avatar answered Mar 20 '26 15:03

profesor79



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!