First, preface
The date type in MongoDB is no time zone concept, the default storage is Isodate ("2018-04-02t13:19:16.418z") This format of 0 time zone, eight hours later than Beijing time.
There is a problem in the work, how to query the document record 30 seconds less than the current time?
Does the time zone issue matter? How do you compare date types? How do you mean the current time? How will the date be added and reduced?
Ii. programme 1, Time zone issues
In fact, the time zone problem is not a problem to be concerned about, because MongoDB provides a JavaScript shell window, support JS syntax. MongoDB automatically handles time zone issues between the JS date type and the MongoDB date type when comparing date types.
2, how to indicate the current time
Now that we have a Javascript shell window, is it not a trivial task to represent the current time? A direct new Date () is done!
3. Implementation date plus minus
With the JS syntax, the date of the addition and subtraction is not particularly difficult to think about it?
1 var New Date (); 2 Now.setseconds (Now.getseconds ()-30);
4. Implement Query
Everything is ready, only owed the East wind. Let's take a look at what the last MongoDB query looks like, right?
1 var New Date (); 2 Now.setseconds (Now.getseconds ()-+); 3 Db.driverLocation.find ({"Uploadtime": {"$gte": now}). Sort ({"Uploadtime":-1});
MongoDB Date type Query