Search code examples
mongodbmongodb-queryaggregation-frameworkstring-to-datetime

mongoDB multiple-levels nested string conversion to date objects


I have a mongoDB containing entries like this:

{ "_id" : ObjectId("5bdb6a44d9b2d4645509db2e"), 
"crs" : { "type" : "name", 
         "properties" : { "name" : "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"type" : "FeatureCollection", 
"features" : [ { "geometry" : { "type" : "Point", "coordinates" : [ 45,66 ] }, 
"type" : "Feature", 
"id" : 50,  
"properties" : { "fogClass" : 0, "_note" : "movable", "fileLocation" : "blah.jpg", "timeStamp" : "2018-11-01 14:51:00", "predFALSE" : 0.998167, "ipAddr" : "http://abcd.ef", "longitude" : "45", "predTRUE" : 0.001833, "cameraID" : "IDABC", "originalPath" : "originalBlah.jpg", "location" : "location1", "latitude" : "66" } } ] }

I would like to perform timestamp-based queries but I have understood that the timestamp have to be mongoDB timestamp objects and not strings as they are now.

I have found that this function might help in converting to mongoDB date objects https://docs.mongodb.com/manual/reference/operator/aggregation/toDate/ and it works on a toy example with a very simple (not-nested) JSON structure.

When I perform the same operation to convert to date the timeStamp field in my dataset (it is stored in the mongoDB in a collection named "collection") and create a new field for that called "timeMongo" as follows:

db.collection.aggregate({$addFields:{timeMongo:{"$toDate":"$features.properties.timeStamp"}}})

I get the following error:

 [js] Error: command failed: {
    "ok" : 0,
    "errmsg" : "Unsupported conversion from array to date in $convert with no onError value",
    "code" : 241,
    "codeName" : "ConversionFailure"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:534:17
assert.commandWorked@src/mongo/shell/assert.js:618:16
DB.prototype._runAggregate@src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1062:12
@(shell):1:1

Looking forward to any help or tips how to make it work. Thanks in advance!


As asked by Anthony here is a toy example and how it should work: this is an example collection with a timeStamp field as string

db.testColl3.find()
{ "_id" : ObjectId("5c1e7fc6e9739a0c2ef3d7fc"), "item" : "card", "qty" : 15, "timeStamp" : "2018-12-20 08:00:00" }

then once I issue the command:

db.testColl3.aggregate({$addFields:{timeMongo:{$toDate:"$timeStamp"}}})

I get this response:

{ "_id" : ObjectId("5c1e7fc6e9739a0c2ef3d7fc"), "item" : "card", "qty" : 15, "timeStamp" : "2018-12-20 08:00:00", "timeMongo" : ISODate("2018-12-20T08:00:00Z") }

which is what I would like to obtain


Solution

  • Your date is inside the array that's why it is throwing error Unsupported conversion from array to date.

    You need to iterate over the features array using $map and then need to add the field timeMongo converted from string to date

    db.collection.aggregate([
      { "$addFields": {
        "features": {
          "$map": {
            "input": "$features",
            "in": {
              "$mergeObjects": [
                "$$this",
                {
                  "timeMongo": {
                    "$toDate": "$$this.properties.timeStamp"
                  }
                }
              ]
            }
          }
        }
      }}
    ])