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. 使用text-shadow可以給圖片加陰影嗎?2. angular.js - angularjs如何傳遞id給另一個視圖 根據id獲取json數據?3. 數據庫無法進入4. java - StringBuffer轉成String,可以不同過tostring,而是通過+“”的方式轉換嗎?5. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?6. java - HashSet<int> 為何有錯誤?7. mysql - 記得以前在哪里看過一個估算時間的網站8. 請問一下各位老鳥 我一直在學習獨孤九賤 現在是在tp5 今天發現 這個系列視頻沒有實戰9. python - linux怎么在每天的凌晨2點執行一次這個log.py文件10. select - mysql怎么搜索一個字符串指定位置之后兩位
