简体   繁体   中英

How to change the value of a field in a stream of tweets before storing in MongoDB using NodeJS

I am trying to change the value of a specific field in a stream of tweets before storing it in MongoDB. For example, the streams I get in twitter contains a field "created_at" : "Wed Jan 24 15:25:20 +0000 2018" and I want to convert this into an ISO date using this code

var now = new Date("Wed Jan 24 15:25:20 +0000 2018");
var isoString = now.toISOString();

However I have no idea how and what to do. I've searched for similar questions but I couldn't find one. Here's my entire code in streaming, saving to mongoDB but without the "modifying the field first before saving."

    var mongoose = require('mongoose'),
    Twit = require('twit'),
    fs = require('fs');
mongoose.connect('mongodb://localhost/insert_sample');


var T = new Twit({
  consumer_key: '',
  consumer_secret: '',
  access_token: '',
  access_token_secret: ''
})

var Schema = mongoose.Schema;

// create a schema
var userSchema = new Schema({}, {"strict": false, collection: 'sample_collection', versionKey: 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 sanFrancisco = [ 116.8127, 4.4681, 127.492047, 19.594594 ]
var stream = T.stream('statuses/filter', { track: 'philippines vacation, itsmorefuninthephilippines, philippines, boracay, palawan, chocolate hills, mindanao, luzon, visayas, dakak' })

stream.on('tweet', function (obj) {

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

Is it what you're looking for ?

stream.on('tweet', function (obj) {
  obj.created_at = new Date(obj.created_at).toISOString();
  var TwitterData = new User(obj); // create object 
  TwitterData.save(); // save data to DB
  console.log(obj);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM