node.js - 使用mongoose的cursor的問題
問題描述
自己測試過成功的樣例,自己新建了個(gè)集合插入了幾條數(shù)據(jù):
//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); }});
數(shù)據(jù)是可以都拿到的
但是我去試自己爬蟲爬到的數(shù)據(jù)集合,只取4條數(shù)據(jù)出來,就有問題:
//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); }});
然后卡這兒就不會(huì)動(dòng)了,代碼都是一致的,怎么就取不出數(shù)據(jù)來呢,求解
問題解答
回答1:感覺你遇到的情況是cursor是空的。
檢查一下:
1、在mongo下面使用相同的條件查詢一下,看是否有返回的文檔。
相關(guān)文章:
1. html5 - 如何實(shí)現(xiàn)帶陰影的不規(guī)則容器?2. javascript - 循環(huán)嵌套多個(gè)promise應(yīng)該如何實(shí)現(xiàn)?3. mysql優(yōu)化 - 關(guān)于mysql分區(qū)4. css - 移動(dòng)端字體設(shè)置問題5. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個(gè)app?6. css3 - rem布局下,用戶瀏覽器的最小字號(hào)是12px怎么辦?7. vue.js - vue 打包后 nginx 服務(wù)端API請(qǐng)求跨域問題無法解決。8. 請(qǐng)教各位大佬,瀏覽器點(diǎn) 提交實(shí)例為什么沒有反應(yīng)9. 前端 - IE9 css兼容問題10. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出
