node.js - 使用mongoose的cursor的问题
问题描述
自己测试过成功的样例,自己新建了个集合插入了几条数据:
//testSchema.jsvar mongoose = require(’./db.js’), Schema = mongoose.Schema;var TestSchema = new Schema({ name: {type: String}, age: {type: Number}})var TestModel = mongoose.model(’stream’, TestSchema, ’stream’);var cache = [];var co = require(’co’);co(function*() { 'use strict'; const cursor = TestModel.find({}).cursor(); for (let doc = yield cursor.next(); doc != null; doc = yield cursor.next()) {console.log(doc); }});
数据是可以都拿到的
但是我去试自己爬虫爬到的数据集合,只取4条数据出来,就有问题:
//test.jsvar mongoose = require(’./db.js’), Schema = mongoose.Schema;var LagouSchema = new Schema({ name: {type: String}, cid: {type: Number}, process: {type: String}, content: {type: String}, url: {type: String}, tag: {type: String}, total: {type: Number}, salary: {type: Array}});var Lagou = mongoose.model(’lagou’, LagouSchema, ’lagou’);var co = require(’co’);co(function*() { 'use strict'; const cursor = Lagou.find({’cid’: {$gte:22777, $lte:22780}}).cursor(); for (let doc = yield cursor.next(); doc != null; doc = yield cursor.next()) {console.log(doc); }});
然后卡这儿就不会动了,代码都是一致的,怎么就取不出数据来呢,求解
问题解答
回答1:感觉你遇到的情况是cursor是空的。
检查一下:
1、在mongo下面使用相同的条件查询一下,看是否有返回的文档。
相关文章:
1. mysql - 我用SQL语句 更新 行的时候,发现全部 中文都被清空了,请问怎么解决?2. mysql主从,从库锁表会导致复制阻塞吗?3. mysql中的join on查询语句的on能否改为where4. javascript - 小米浏览器中,图片导致fixed定位的元素无法显示5. java - yuicompressor-maven-plugin 合并可用却不压缩, 哪配置不对?6. update方法不能更新字段值为0的数据7. javascript - ES5的闭包用ES6怎么实现8. word-wrap该如何使用?9. javascript - 为什么!function foo(){}返回false,!function foo(){}()返回true?10. html - 特殊样式按钮 点击按下去要有凹下和弹起的效果
