使用Mongoose

时间:2018-01-18 13:53:43

标签: javascript node.js mongodb twitter stream

所以我想使用Mongoose从MongoDB中的Twitter Streaming API保存数据流。不知何故,它不会保存所有数据,并且当我运行我的代码时,它只会从流中的无限量数据中保存1个文档(这是不完整的):

var mongoose = require('mongoose'),
TwitterStream = require('twitter-stream-api'),
fs = require('fs');
mongoose.connect('mongodb://localhost/insert_sample');

var keys = {
consumer_key : "",
consumer_secret : "",
token : "",
token_secret : ""
};

var Schema = mongoose.Schema;

// create a schema
var userSchema = new Schema({}, {"strict": false});

// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', userSchema);

// make this available to our users in our Node applications
module.exports = User;

var Twitter = new TwitterStream(keys, false);
Twitter.stream('statuses/filter', {
track: 'travel'
});

// create a new user called twitter
var Twitter = new User();

// call the built-in save method to save to the database
Twitter.save(function(err) {
if (err) throw err;

console.log('User saved successfully!');
});

输出命令行:User saved successfully!

当我尝试使用db.users.find()在MongoDB shell中查询时,结果如下:

{ "_id" : ObjectId("5a60a5558f4f1d21bcadd72b"), "__v" : 0 }

应该是这样的:

{
"created_at": "Fri Aug 04 05:42:03 +0000 2017",
"id": 893346273255866400,
"id_str": "893346273255866368",
..........and soooo many more attributes }

EDIT !!!对不起,我不小心点击了输入并发布了它,没有其他需要清楚的东西。我的坏。

1 个答案:

答案 0 :(得分:0)

您正在覆盖您的Twitter变量并保存用户数据空对象。

// create a new user called twitter
var Twitter = new User();

// call the built-in save method to save to the database
Twitter.save(function(err) {
if (err) throw err;

创建流后,您需要侦听数据事件并使用该数据保存到数据库中。

删除上面的行并使用类似的内容

 // listen for data from stream
    Twitter.on('data', function (obj) {

        var data =  obj.toString('utf8');
        console.log(data);
        var TwitterData = new User(data); // create object 
        TwitterData.save(); // save data to DB

    });

希望它有帮助!