I try to abort a query after it works specific time. So ı use mongodb maxTimeMS() method for my query. But it is not working.
I except that this query will stop after 5 second but keep working. how ı terminate my query after it works specific time
var sum = 0 ;
var inbox=db.getMongo().getDB("xxxx").getCollection("Inbox");
var oldInbox=db.getMongo().getDB("xxxx").getCollection("oldInbox");
var inboxCount = inbox.count();
var oldCount = oldInbox.count();
var dif = inboxCount - oldCount ;
if ( dif > 10000000 ){
var cursor = inbox.find().maxTimeMS(5000);
cursor.sort({_id : -1}).forEach(function(uidDoc) {
var uid = uidDoc.uid;
var docsToMigrate = [];
var idsToRemove = [];
inbox.find({uid : uid}).sort({_id : -1}).skip(10).forEach(function(doc) {
docsToMigrate.push(doc);
idsToRemove.push(doc._id);
var x = doc._id;
});
oldInbox.insert(docsToMigrate);
inbox.remove({_id : {$in : idsToRemove}});
sum = sum + idsToRemove.length;
if ( x = 0 )
{
print(sum);
cursor.close();
}
});
};