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.