user1578872
user1578872

Reputation: 9018

mongodb java query where clause for date

I am new to MongoDB and not sure how to achieve the below. One of the document from the collection,

{ 
    "_id" : ObjectId("56cf4cf2997964f0d91aa373"), 
    "id" : "A", 
    "status" : "open", 
    "createdTime" : NumberLong(1456426212898)
}

I would like to fetch data with the following where query,

where - status is open && (createdTime + 10 mins) < currentTime

The below code works for the status. But not sure how to include the time as shown above.

for open `status` - {"status":"open"}

Java code,

    final Bson statusFilter = eq("status", "open");

    final Document projection = new Document();

    final Document sort = new Document();
    sort.append(CREATED_TIME, -1);

    final FindIterable<Document> operation = collection.find(filter).projection(projection).sort(sort);

But not sure, how to achieve ( createdTime + 10 mins) < currentTime.

Thanks

Upvotes: 1

Views: 544

Answers (1)

Mansoor
Mansoor

Reputation: 1259

It should be

{ $and:[{ "status":"open"},{"createdTime":{$lt:new Date().getTime() - (1000 * 60 * 10)}} ] }

The query is similar to where status=open && createdTime<(currentTime-10mins)

Note: createdTime+10mins < currentTime can be written as createdTime < currentTime-10 mins

Upvotes: 3

Related Questions