简体   繁体   English

我如何终止游标 mongodb

[英]How do ı terminate cursor mongodb

I try to abort a query after it works specific time.我尝试在特定时间工作后中止查询。 So ı use mongodb maxTimeMS() method for my query.所以我使用 mongodb maxTimeMS() 方法进行查询。 But it is not working.但它不起作用。

I except that this query will stop after 5 second but keep working.我除了这个查询将在 5 秒后停止但继续工作。 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();
    }
});
};

Based on documentation here , maxTimeMS is a limit on processing time within the database.根据此处的文档, maxTimeMS是对数据库内处理时间的限制。 This will cover query planning, execution, and data serialization, but the timer stops when the cursor is idle.这将涵盖查询计划、执行和数据序列化,但计时器在游标空闲时停止。 This is not wall time and means that it does not account for the execution time within your forEach callback.这不是挂墙时间,这意味着它不考虑forEach回调中的执行时间。 Since your query seems quite simple, it will likely take much more time than the maxTimeMS limit for it to stop.由于您的查询看起来很简单,它可能需要比maxTimeMS限制更多的时间才能停止。

So to answer your question, you'll need to keep a timer elsewhere and manage it yourself using paginated queries .因此,要回答您的问题,您需要在别处保留一个计时器并使用分页查询自行管理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM