想读取mongodb中爬虫存储的数据,用nodejs的mongoose工具读不到数据,求解?

db.js

var mongoose = require('mongoose'),
    DB_URL = 'mongodb://localhost:27017/result';

mongoose.connect(DB_URL);
//连接成功
mongoose.connection.on('connected', function(){
    console.log('Mongoose connection open to ' + DB_URL);
})
//连接异常
mongoose.connection.on('error', function(err){
    console.log('Mongoose connection error: ' + err);
})
//连接断开
mongoose.connection.on('disconnected', function(){
    console.log('Mongoose connection disconnected');
})

module.exports = mongoose;

model.js

var 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);

function update(conditions, update){
    Lagou.update(conditions, update, function(err, res){
        if(err) console.log('Error:' + err);
        else console.log('Res:' + res);
    })
}

function del(conditions){
    Lagou.remove(conditions, function(err, res){
        if(err) console.log('Error:' + err);
        else console.log('Res:' + res);
    })
}

function find(conditions, callback){
    Lagou.find(conditions, function(err, res){
        if(err) console.log('Error:' + err);
        else callback(res);
    })
}

module.exports = {
    find: find,
    del: del,
    update: update
}

lagou.js

var db = require('./model.js');

db.find({'name': "仁维"}, function(res){
    console.log(res);
})

这是之前爬虫爬到的数据

clipboard.png

运行node lagou.js之后拿到的数据是空

clipboard.png

求解,是schema的问题吗?

阅读 3.9k
2 个回答

请参考下面的这个帖子:

https://segmentfault.com/q/10...

var User = mongoose.model('User', userSchema,'user');

这个部分容易出现问题;请对应修改过来。

供参考。

看起来没有问题。
是同一个集合吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题