MongoDB Date type Query

Source: Internet
Author: User
Tags mongodb mongodb query

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.