node.js - 使用mongoose的cursor的問(wèn)題
問(wèn)題描述
自己測(cè)試過(guò)成功的樣例,自己新建了個(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ù)是可以都拿到的
但是我去試自己爬蟲(chóng)爬到的數(shù)據(jù)集合,只取4條數(shù)據(jù)出來(lái),就有問(wèn)題:
//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ù)來(lái)呢,求解
問(wèn)題解答
回答1:感覺(jué)你遇到的情況是cursor是空的。
檢查一下:
1、在mongo下面使用相同的條件查詢一下,看是否有返回的文檔。
相關(guān)文章:
1. python - [已解決]flask QQ郵箱mail2. html - eclipse 標(biāo)簽錯(cuò)誤3. 主題切換問(wèn)題,用過(guò)別人的webapp在后臺(tái)切換模板主題后手機(jī)端打開(kāi)網(wǎng)頁(yè)就是切換到的主題了4. javascript - 小程序中遇到j(luò)s執(zhí)行時(shí)序問(wèn)題5. mysql 5個(gè)left關(guān)鍵 然后再用搜索條件 幾千條數(shù)據(jù)就會(huì)卡,如何解決呢6. 按照本節(jié)給的代碼“膽小如鼠”并不能變成紅色7. mysql - 在不允許改動(dòng)數(shù)據(jù)表的情況下,如何優(yōu)化以varchar格式存儲(chǔ)的時(shí)間的比較?8. python3.x - git bash如何運(yùn)行.bat文件?9. javascript - 為什么在谷歌控制臺(tái) 輸出1的時(shí)候,輸出的1立馬就不見(jiàn)了10. javascript - position fixed;設(shè)置了height 100 卻不是瀏覽器可視窗口的寬高,求大神釋疑。
