2

次のようなエントリを含むmongoDBがあります。

{ "_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" } } ] }

タイムスタンプ ベースのクエリを実行したいのですが、タイムスタンプは現在の文字列ではなく、mongoDB タイムスタンプ オブジェクトである必要があることを理解しています。

この関数は、mongoDB 日付オブジェクトhttps://docs.mongodb.com/manual/reference/operator/aggregation/toDate/への変換に役立つ可能性があることがわかりました。非常に単純な (ネストされていない) おもちゃの例で機能します。 JSON 構造。

データセットの timeStamp フィールドを日付に変換する同じ操作を実行すると (「コレクション」という名前のコレクションの mongoDB に格納されます)、次のように「timeMongo」という名前の新しいフィールドを作成します。

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

次のエラーが表示されます。

 [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

それを機能させるためのヘルプやヒントを楽しみにしています。前もって感謝します!


Anthony が尋ねたように、おもちゃの例とそれがどのように機能するかを次に示します。これは、timeStamp フィールドを文字列として持つサンプル コレクションです。

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

次に、コマンドを発行したら:

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

私はこの応答を受け取ります:

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

それは私が手に入れたいものです

4

1 に答える 1