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); }});
然后卡這兒就不會動(dòng)了,代碼都是一致的,怎么就取不出數(shù)據(jù)來呢,求解
問題解答
回答1:感覺你遇到的情況是cursor是空的。
檢查一下:
1、在mongo下面使用相同的條件查詢一下,看是否有返回的文檔。
相關(guān)文章:
1. 如何解決Centos下Docker服務(wù)啟動(dòng)無響應(yīng),且輸入docker命令無響應(yīng)?2. 我在centos容器里安裝docker,也就是在容器里安裝容器,報(bào)錯(cuò)了?3. javascript - 微信h5發(fā)送圖文信息,部分設(shè)備點(diǎn)擊“發(fā)送”按鈕時(shí)沒反應(yīng),問題較難重現(xiàn),如何能找到可能存在問題的點(diǎn)?4. javascript - 音樂播放器-圖片旋轉(zhuǎn)5. android - 為 AppBarLayout 設(shè)置的背景圖片 TransitionDrawable 為什么只在第一次打開的時(shí)候有效?6. javascript - js中遞歸與for循環(huán)同時(shí)發(fā)生的時(shí)候,代碼的執(zhí)行順序是怎樣的?7. MySQL timestamp的默認(rèn)值怎么設(shè)置?8. android - 使用百度sdk調(diào)用SDKInitializer.initialize(this)時(shí)報(bào)錯(cuò)?9. objective-c - IOS 分享到微信 提示 應(yīng)用消息數(shù)據(jù)錯(cuò)誤10. docker 17.03 怎么配置 registry mirror ?
