Meteor js cursor hasNext() and next()?
Meteor js cursor hasNext() and next()?
problem
我有一个潜在的大查找查询,一旦满足我的服务器"javascript"条件,它就必须停止,例如我的服务器已经有文档结果来找出解决方案。
这里的想法是从 mongo 迭代一个光标,点击 mongo"按需",有点"流式处理",所以我的服务器或 mongo 都不必一次获取所有结果。
预期行为
- 查询并获取游标 ( r = Collection.find() )
- 检查下一个 ( r.hasNext() )
- 获取下一个 ( r.next() )
- 满足我的要求后,停止(删除R)
示例代码
Collection.find().forEach(function(doc) {
doc = do_my_business( doc );
if (doc.found) {
// hey mister cursor, go home and die! don't touch my mongo anymore!
return false;
}
});
真正的问题
我创建了另一个问题,实际上是我在这里结束的方式:https://stackoverflow.com/questions/30836586/mongodb-find-near-until-maxdistance-or-at-least-x-records
流星光标不是 mongo 光标。流星光标没有 hasNext 或 Next。所以,这实际上只是一个关于forEach
的问题,这与ES5 forEach
相同。因此,您可以执行以下操作:
1.将所有文档提取到一个数组中并对其运行 for 循环,在满足条件时中断。
2.当满足条件时转动标志并在函数开始时检查:
if (isHappy) return;
doc = do_my_business( doc );
if (doc.found) isHappy = true;
3. 使用尝试捕获循环:
foo = {};
try {
Collection.find().forEach(function(doc) {
doc = do_my_business( doc );
if (doc.found) {
throw foo;
}
});
} catch(e) {
if (e !== foo) {
throw e;
}
}
4.忘记forEach垃圾并使用带有findOne
+的$where
(做一个find().limit(1).explain()
,这样你可以看到它不会触及每个文档。
在数据库中 5.Store 一些东西,这样你就不必在光标上使用javascript。应该避免像瘟疫一样在服务器上的每个文档上重复javascript循环。存储很便宜。处理,不是那么多。
相关文章:
- Selenium WebDriver and JavaScript change
- ES6生成器:.next()的输入值
- PHP and Javascript functions
- Javascript Return and if/else
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- TimelineJS and AngularJS
- timeago.js with datatable and PHP
- javascript button ajax and php
- Ajax and Json with Rails
- Combine onload, onresize and onclick
- php布尔值's小写AND大写和数字布尔值'可以接受
- setTimeout and V8
- jQuery next().addClass() and prev().addClass() issue
- Accessing $(this).next and slideDown
- jquery next () and setInterval ()
- jquery lightbox previous and next buttons
- Nodejs and Connect "next" functionality
- next(), closest() and find()
- Meteor js cursor hasNext() and next()?