0

I have a Mongo collection that stored a date field as a string. I ran the query below to convert this field into an ISODate, but it failed. However, there are quite a few records that now show as an ISODate.

How do I query to find all documents that are an ISODate format and all documents that are still a string?

db.TestCollection.find({}).forEach(function(doc) {
    doc.LastUpdated = new Date(doc.LastUpdated );

    db.TestCollection.save(doc);

    });

The original question has been answered, but the follow up question is: Is there a way to aggregate the type of data present in the column? I would like to return a count of each type of data in the column. I have seen type string and type date, but want to verify there isn't any other data types.

Here's roughly what I have tried:

db.TestCollection.aggregate([{
    $project: {
       LastUpdated : { $type: "$LastUpdated " }
    }
}])
ferensilver
  • 341
  • 2
  • 4
  • 17

0 Answers0