繁体   English   中英

在使用NodeJS存储在MongoDB中之前,如何更改推文流中字段的值

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

我试图在将其存储在MongoDB中之前更改一条推文流中特定字段的值。 例如,我在twitter中获取的流包含一个字段"created_at" : "Wed Jan 24 15:25:20 +0000 2018" ,我想使用此代码将其转换为ISO日期

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

但是我不知道该如何做。 我已经搜索了类似的问题,但找不到。 这是我流式传输中的全部代码,保存到mongoDB中,但没有“保存前先修改字段”。

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

您在找什么吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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