I have a collection in mongodb, same as following in SQL,

Orders[orderId, ... ,status, orderDate, expiredTimeInSeconds].

I need to find all expired orders, that is, Orders with (orderDate + expiredTimeInSeconds) > current_date.

I used following query to get first 100 records meeting above criteria,

db.getCollection('orders').aggregate(
   [
     { $match : {status:"C"}},
     { $addFields: {expiryDate: { $add: [ "$orderDate", {$multiply:["$expiredTimeInSecond",1000]} ] } } },
     { $match : {expiryDate: { $gt : new Date()}}},
     { $limit : 100 }
   ]
)

But, the problem is, aggregates is taking lot of time to run on huge database, can this be achieved using other less time consuming mongoDB query/feature?

P.S. Preferable , if the query has Java support.

0 Answers