-1

Hello I am trying to update in MongoDB all my JSON documents from a string format ("Fri May 20 09:54:13 +0000 2016") to a ISODate format (YYYY-MM-DD HH:MM:SS). To accomplish this I use sub-string and a home made function. This works fine and I see that the console translate it as it should be.

But when I update the records I see the update is not working. What can I do to make it work.

Many Thanks.

sample json document

{
  "_id" : ObjectId("573edec6ac5e621961f963f2"),
  "contributors" : null,
  "truncated" : false,
  "text" : "Namens de verenigde marathonschaatsteams: gefeliciteerd @KNSB, @KPN en @Versteegen met het blijvend verbinden van schaatsend Nederland!",
  "is_quote_status" : false,
  "in_reply_to_status_id" : null,
  "id" : NumberLong("733596684547018752"),
  "favorite_count" : NumberInt("0"),
  "source" : "<a href=\"https://about.twitter.com/products/tweetdeck\" rel=\"nofollow\">TweetDeck</a>",
  "timestamp_ms" : "1463738053820",
  "in_reply_to_screen_name" : null,
  "id_str" : "733596684547018752",
  "retweet_count" : NumberInt("0"),
  "in_reply_to_user_id" : null,
  "favorited" : false,
  "geo" : null,
  "in_reply_to_user_id_str" : null,
  "lang" : "nl",
  "created_at" : "Fri May 20 09:54:13 +0000 2016",
  "filter_level" : "low",
  "in_reply_to_status_id_str" : null,
  "place" : null
}

In try thescript based on the advice of the comments on this solution:

Multiply field by value in Mongodb

Update script in Mongobooster

db.eval(function() { 
    db.STG_LEADS_AUTOSCHADE.find().forEach(function(e) {
        e.created_at = new Date(e.created_at);
        db.collection.save(e);
});

});

result

WARNING: db.eval is deprecated

enter image description here

But the result is that the created_at field is not updated and is still a string.

Erik hoeven
  • 1,442
  • 6
  • 26
  • 41
  • And the documents presently look like what exactly? Your code does not exactly help us to "reverse engineer" if it "does not work". No idea what `.updateupdate` is, and that looks like a typo, either in your post or your actual code, which would be an interpretation of "does not work". You need to be a bit more specific about what exactly is not working and provide enough information so that others can help, if you want help that is. – Neil Lunn Aug 09 '17 at 11:56
  • Thanks for your fast reply -Neil Lunn. I improved my question with an example of the json document and removed the typo – Erik hoeven Aug 09 '17 at 12:07
  • Well for me `new Date("Fri May 20 09:54:13 +0000 2016")` returns `ISODate("2016-05-20T09:54:13Z")` which would appear to be correct and all you really need to do. Is it not? – Neil Lunn Aug 09 '17 at 12:11
  • This does not work ether -Neil Lunn. It keeps hanging at the same document. Same result i have on the first method! – Erik hoeven Aug 09 '17 at 12:20
  • Then perhaps you should make sure the key is present and that it is actually a string. `dbTweets.find({ "created_at": { "$type": 2 } })`. Also there is absolutely no need to write this in nodejs. Just do it in the shell. You are not using callbacks correctly, and it's really not worth the time explaining it too you simply to fix the dates. Just run the conversion from the mongo shell and use `new Date()` instead of your own custom functions. – Neil Lunn Aug 09 '17 at 12:25
  • See [Update MongoDB field using value of another field](https://stackoverflow.com/questions/3974985/update-mongodb-field-using-value-of-another-field) for vast improvements on how to handle this type of update. – Neil Lunn Aug 09 '17 at 12:26
  • Thanks for the advice -Neil Lunn at first i going to think that the solutions you adviced was going to work. But then the console prompted that it was deprecated (see edited question). But the result keep the same. – Erik hoeven Aug 10 '17 at 06:30

1 Answers1

0

Eventualy are solve the problem by creating a loop (forEach)

db.STG_LEADS_AUTOSCHADE.find().forEach(function(e){
    var d = e.created_at
    db.STG_LEADS_AUTOSCHADE.update({_id: e._id},{$set{created_at: new Date(d)}})
})
Erik hoeven
  • 1,442
  • 6
  • 26
  • 41