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. 點擊頁面就自動輸入到mysql.求解2. java - 多叉樹求值,程序高手,算法高手看過來3. node.js - nodejs使用formidable上傳文件問題4. java如何生成token?5. java - IDEA從SVN檢出項目 并在tomcat上運行 求詳細流程6. linux - 新手-----nginx怎么配置請求轉發?7. node.js - 帶有node_modules目錄的項目,用phpstorm打開速度極慢,怎么解決?8. ubuntu為什么這個文件夾里面的文件都被鎖上了?9. javascript - windos下第一次用Django無法正確創建工程目錄10. JAX-RS,GlassFish,Eclipse。簡單的Web服務不起作用
