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. node.js - webpack --watch以后报错env: node: No such file or directory2. 静态资源文件引入无效3. node.js - 在node中,一个加密操作用时69ms,是否可以接受?4. javascript - 表单提交后,后台直接返回页面,应该如何处理?5. javascript - htaccess rewrite 的问题6. javascript - UI样式计算过多导致浏览器加载过慢,IE尤其严重,求前辈指导7. css3:flex排版问题8. javascript - <div onclick="fun()"></div>这种fun函数只有写成全局函数才有效吗?9. javascript - jQuery 获取表格所有数据10. javascript - webpack 多入口文件生成HTML文件;

网公网安备