node.js - 使用mongoose的cursor的问题
巴扎黑
巴扎黑 2017-04-17 15:52:07
[Node.js讨论组]

自己测试过成功的样例,自己新建了个集合插入了几条数据:

//testSchema.js
var 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.js
var 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下面使用相同的条件查询一下,看是否有返回的文档。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号