Javascript MangGDB使用NoDEJS本地驱动程序在查找排序结果中间获取元素 我有这个大的集合,我需要用一个字段BO对它进行排序,并获取结果中间的文档,而不循环遍历所有的结果。 所以对于这个例子: [ {_id: 5d36b8f79600563a2cecd990, file_id: '5d2393b912c90b2b9c51d0eb', cant: '1', bo: 100 }, {_id: 5d36b8f79600563a2cecd991, file_id: '5d2393b912c90b2b9c51d0eb', cant: '2', bo: 200 }, {_id: 5d36b8f79600563a2cecd992, file_id: '5d2393b912c90b2b9c51d0eb', cant: '3', bo: 300 }, {_id: 5d36b8f79600563a2cecd993, file_id: '5d2393b912c90b2b9c51d0eb', cant: '4', bo: 400 }, {_id: 5d36b8f79600563a2cecd994, file_id: '5d2393b912c90b2b9c51d0eb', cant: '5', bo: 500 }, {_id: 5d36b8f79600563a2cecd995, file_id: '5d2393b912c90b2b9c51d0eb', cant: '7', bo: 660 }, {_id: 5d36b8f79600563a2cecd996, file_id: '5d2393b912c90b2b9c51d0eb', cant: '6', bo: 670 }, {_id: 5d36b8f79600563a2cecd997, file_id: '5d2393b912c90b2b9c51d0eb', cant: '8', bo: 680 }, {_id: 5d36b8f79600563a2cecd998, file_id: '5d2393b912c90b2b9c51d0eb', cant: '9', bo: 550 }, {_id: 5d36b8f79600563a2cecd999, file_id: '5d2393b912c90b2b9c51d0eb', cant: '0', bo: 700 } ]

Javascript MangGDB使用NoDEJS本地驱动程序在查找排序结果中间获取元素 我有这个大的集合,我需要用一个字段BO对它进行排序,并获取结果中间的文档,而不循环遍历所有的结果。 所以对于这个例子: [ {_id: 5d36b8f79600563a2cecd990, file_id: '5d2393b912c90b2b9c51d0eb', cant: '1', bo: 100 }, {_id: 5d36b8f79600563a2cecd991, file_id: '5d2393b912c90b2b9c51d0eb', cant: '2', bo: 200 }, {_id: 5d36b8f79600563a2cecd992, file_id: '5d2393b912c90b2b9c51d0eb', cant: '3', bo: 300 }, {_id: 5d36b8f79600563a2cecd993, file_id: '5d2393b912c90b2b9c51d0eb', cant: '4', bo: 400 }, {_id: 5d36b8f79600563a2cecd994, file_id: '5d2393b912c90b2b9c51d0eb', cant: '5', bo: 500 }, {_id: 5d36b8f79600563a2cecd995, file_id: '5d2393b912c90b2b9c51d0eb', cant: '7', bo: 660 }, {_id: 5d36b8f79600563a2cecd996, file_id: '5d2393b912c90b2b9c51d0eb', cant: '6', bo: 670 }, {_id: 5d36b8f79600563a2cecd997, file_id: '5d2393b912c90b2b9c51d0eb', cant: '8', bo: 680 }, {_id: 5d36b8f79600563a2cecd998, file_id: '5d2393b912c90b2b9c51d0eb', cant: '9', bo: 550 }, {_id: 5d36b8f79600563a2cecd999, file_id: '5d2393b912c90b2b9c51d0eb', cant: '0', bo: 700 } ],javascript,node.js,mongodb,performance,Javascript,Node.js,Mongodb,Performance,我只需要拿到文件 {_id: 5d36b8f79600563a2cecd998, file_id: '5d2393b912c90b2b9c51d0eb', cant: '9', bo: 550 } 所以中间或者中间之后的第一个 我试着在一个数组中获得所有结果,发现它是中间的 var half_length=Math.ceilmyArray.length/2; 然后得到那个元素,但是这个操作太慢了 分贝 .收藏“历史” .find{file_id:'5d2393b912c90b2b9c51d0e

我只需要拿到文件

{_id: 5d36b8f79600563a2cecd998, file_id: '5d2393b912c90b2b9c51d0eb', cant: '9', bo: 550 }
所以中间或者中间之后的第一个

我试着在一个数组中获得所有结果,发现它是中间的 var half_length=Math.ceilmyArray.length/2; 然后得到那个元素,但是这个操作太慢了

分贝 .收藏“历史” .find{file_id:'5d2393b912c90b2b9c51d0eb'} .sort{bo:1} .ToArrayFunction错误、结果{ 如果犯了错误,就扔出错误; var half_length=Math.ceilresult.length/2; console.logresult[半_长度]; }; };
我需要找到一个在查找/排序查询结果中间的一个记录,而不把它保存到一个数组中。如果可能的话

找到带有所需过滤器的文档计数

const count = db.collection('history').count({file_id: '5d2393b912c90b2b9c51d0eb'});
const skipCount = Math.ceil(count/2) - 1;
为了得到中间的结果

db.collection('history').find({file_id: '5d2393b912c90b2b9c51d0eb'}).sort({ bo: 1 }).skip(skipCount).limit(1);
使用节点mongodb本机API,可以将查询编写为

   db.collection('history').countDocuments({ file_id: '5d2393b912c90b2b9c51d0eb' }, function(err, count) {
        const skipCount = Math.ceil(count/2) - 1;
        db.collection('history').find({ file_id: '5d2393b912c90b2b9c51d0eb' }).sort({ bo: 1 }).skip(skipCount).limit(1) .toArray(function(err, result) {
            if (err) throw err;
            // Here is your result
            console.log(result);
        });
    });

查找具有所需筛选器的文档计数

const count = db.collection('history').count({file_id: '5d2393b912c90b2b9c51d0eb'});
const skipCount = Math.ceil(count/2) - 1;
为了得到中间的结果

db.collection('history').find({file_id: '5d2393b912c90b2b9c51d0eb'}).sort({ bo: 1 }).skip(skipCount).limit(1);
使用节点mongodb本机API,可以将查询编写为

   db.collection('history').countDocuments({ file_id: '5d2393b912c90b2b9c51d0eb' }, function(err, count) {
        const skipCount = Math.ceil(count/2) - 1;
        db.collection('history').find({ file_id: '5d2393b912c90b2b9c51d0eb' }).sort({ bo: 1 }).skip(skipCount).limit(1) .toArray(function(err, result) {
            if (err) throw err;
            // Here is your result
            console.log(result);
        });
    });

我收到一个弃用警告:collection.count已弃用,将在将来的版本中删除。count返回承诺,skipCount返回NaN,我认为我们必须使用回调:db.collection'history'。countDocuments{file_id:'5d23933b912c90b2b9c51d0eb',functionerr count{const skipCount=Math.ceilcount/2-1;}我已经解决了这个问题,在初始计数时使用回调函数,并在回调函数中得到结果的中间部分,您可以编辑您的答案,我会将其标记为已接受:db.collection'history'.countDocuments{file_id:'5d23933b912c90b2b9c51d0eb',functionerr,count{const skipCount=Math.ceilcount/2-1;db.collection'history'.find{prods_manopere_id:'5d2393b912c90b2b9c51doeb'}.sort{bo:1}.skipskipCount.limit1.ToArrayFunction错误,结果{if err throw err;console.logresult;};}谢谢。我收到一个弃用警告:collection.count已弃用,将在将来的版本中删除。count返回一个承诺,skipCount返回NaN,我认为我们必须使用回调:db.collection'history'。countDocuments{file_id:'5d239b912c90b2b9c51d0eb',functionerr count{const skipCount=Math.ceilcount/2-1;}我已经解决了这个问题,在初始计数时使用回调函数,并在回调函数中得到结果的中间部分,您可以编辑您的答案,我会将其标记为已接受:db.collection'history'.countDocuments{file_id:'5d23933b912c90b2b9c51d0eb',functionerr,count{const skipCount=Math.ceilcount/2-1;db.collection'history'.find{prods_manopere_id:'5d2393b912c90b2b9c51d0eb'}。排序{bo:1}。skipskipCount.limit1。ToArrayFunction错误,结果{if err throw err;console.logresult;};}感谢我们。