1

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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This answer is accepted. But would like to know how to use the filed in the query like createdTime+10mins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.