How do I convert a property in MongoDB from text to date type?

Mongodb

Mongodb Problem Overview


In MongoDB, I have a document with a field called "ClockInTime" that was imported from CSV as a string.

What does an appropriate db.ClockTime.update() statement look like to convert these text based values to a date datatype?

Mongodb Solutions


Solution 1 - Mongodb

This code should do it:

> var cursor = db.ClockTime.find()
> while (cursor.hasNext()) {
... var doc = cursor.next();
... db.ClockTime.update({_id : doc._id}, {$set : {ClockInTime : new Date(doc.ClockInTime)}})
... }

Solution 2 - Mongodb

I have exactly the same situation as Jeff Fritz.

In my case I have succeed with the following simpler solution:

db.ClockTime.find().forEach(function(doc) { 
    doc.ClockInTime=new Date(doc.ClockInTime);
    db.ClockTime.save(doc); 
    })

Solution 3 - Mongodb

This is a generic sample code in python using pymongo

from pymongo import MongoClient
from datetime import datetime
 
def fixTime(host, port, database, collection, attr, date_format):
    #host is where the mongodb is hosted eg: "localhost"
    #port is the mongodb port eg: 27017
    #database is the name of database eg : "test"
    #collection is the name of collection eg : "test_collection"
    #attr is the column name which needs to be modified
    #date_format is the format of the string eg : "%Y-%m-%d %H:%M:%S.%f"
    #http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
    client = MongoClient(host, port)
    db = client[database]
    col = db[collection]
    for obj in col.find():
        if obj[attr]:
            if type(obj[attr]) is not datetime:
                time = datetime.strptime(obj[attr],date_format)
                col.update({'_id':obj['_id']},{'$set':{attr : time}})

for more info : http://salilpa.com/home/content/how-convert-property-mongodb-text-date-type-using-pymongo

Solution 4 - Mongodb

Starting Mongo 4.x:

  • db.collection.update() can accept an aggregation pipeline, finally allowing the update of a field based on its current value (Mongo 4.2+).
  • There is a new $toDate aggregation operator (Mongo 4.0).

Such that:

// { a: "2018-03-03" }
db.collection.update(
  {},
  [{ $set: { a: { $toDate: "$a" } } }],
  { multi: true }
)
// { a: ISODate("2018-03-03T00:00:00Z") }
  • The first part {} is the match query, filtering which documents to update (in this case all documents).

  • The second part [{ $set: { a: { $toDate: "$a" } } }] is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline). $set is a new aggregation operator which in this case replaces the field's value. The replaced value being the field itself concerted to an ISODate object. Note how a is modified directly based on its own value ($a).

  • Don't forget { multi: true }, otherwise only the first matching document will be updated. Or alternatively one can use updateMany instead of update: db.collection.updateMany(...).

Solution 5 - Mongodb

If you need to check if the field already has been converted you can use this condition:

/usr/bin/mongo mydb --eval 'db.mycollection.find().forEach(function(doc){
    if (doc.date instanceof Date !== true) {
        doc.date = new ISODate(doc.date);
        db.mycollection.save(doc);
    }
});'

Otherwise the command line may break.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff FritzView Question on Stackoverflow
Solution 1 - MongodbkristinaView Answer on Stackoverflow
Solution 2 - MongodbCigesView Answer on Stackoverflow
Solution 3 - MongodbSalil PanikkaveettilView Answer on Stackoverflow
Solution 4 - MongodbXavier GuihotView Answer on Stackoverflow
Solution 5 - MongodbwebDEVILopersView Answer on Stackoverflow