MongoDB subtraction Query

Source: Internet
Author: User
Tags epoch seconds

For example, each document in the SET has a field indicating the epoch seconds. The task is to subtract the value of this field from the epoch of the current time. If the difference exceeds 300 seconds, it is queried.

Let's take a look at the following example of document data:
 

{"_id" : ObjectId("5271ab5133f6792263dd8e8e"),"address" : "131031000947","description" : "bind","group_id" : ObjectId("505efb8a3b62c5d7bc8c624f"),"last_active_time" : 1386768863,"location" : "test","status" : "offline","user_id" : ObjectId("4ee175ff82bc6273d0d4672f"),"validate_code" : "123456"}

It is assumed that the epoch of the current time is 1386775885

The query statement should be written in this way.

rs1:PRIMARY> db.display.findOne({$where: '(1386775885 - this.last_active_time > 300)'}){"_id" : ObjectId("5271ab5133f6792263dd8e8e"),"address" : "131031000947","description" : "bind","group_id" : ObjectId("505efb8a3b62c5d7bc8c624f"),"last_active_time" : 1386768863,"location" : "test","status" : "offline","user_id" : ObjectId("4ee175ff82bc6273d0d4672f"),"validate_code" : "123456"}

Use $ and to connect two query conditions and use count to display the quantity.

rs1:PRIMARY> db.display.find({$and: [{$where: '(1386775885 - this.last_active_time > 300)'}, {status: "online"}]}).count()0

The key to this query is the use of $ where, where allows JavaScript code execution.

Then $ and.

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.