如何使用 mongo-go-driver 中的 ParseExtJSONArray() 解析聚合管道中的扩展 JSON 日期

How to parse extended JSON Date in aggregation pipeline using ParseExtJSONArray() in mongo-go-driver

我有一个包含日期字段的集合:

{
    "_id" : ObjectId("5b92b359ddceef5b24502834"),
    "dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"),
    yada, yada, yada
}

我正在尝试使用 mongo-go-driver 的 ParseExtJSONArray 函数在 $match 聚合阶段按日期查找。 (我知道如何直接使用 *bson.Array 执行此操作。我在问,所以我知道使用 ParserExtJSONArray 执行此操作的正确方法,或者如果我 运行 遇到限制。)

我已简化此示例并确认它与上述文档不符。

pipeline, err := bson.ParseExtJSONArray(`[
    { "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)

以下在 mongo shell 中不起作用。 (这并不奇怪,因为它会自动转换为 ISODate() 格式)

db.getCollection('received_from_response_queue').aggregate([
    { "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
])

但这确实适用于 mongo shell。

db.getCollection('received_from_response_queue').aggregate([
    { "$match": { "dateTimeGMT": ISODate("2018-08-22T09:29:25.000Z") } }
])

但是这个 returns "pipeline" 中的一个空数组。 (因为 ParseExtJSONArray 不处理 JavaScript)

pipeline, err := bson.ParseExtJSONArray(`[
    { "$match": { "dateTimeGMT":ISODate("2018-08-22T09:29:25.000Z") } }
]`)

因为它随后使用了一个空数组,所以它重新调整了集合中的所有文档。有趣的是,我们尝试匹配的文档中的日期格式不同。

{
    "_id" : { "$oid" : "5b92b359ddceef5b24502834" },
    "dateTimeGMT" : { "$date" : "2018-08-22T05:29:25-04:00" },
    yada yada yada
}

但这也不匹配。

pipeline, err := bson.ParseExtJSONArray(`[
    { "$match": { "dateTimeGMT.$date":"2018-08-22T05:29:25-04:00" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)

这在 mongo shell 中不起作用。

db.getCollection('received_from_response_queue').aggregate([
    { "$match": { "dateTimeGMT.$date":"2018-08-22T05:29:25-04:00" } }
])

有什么见解吗?

背后的想法 MongoDB Extended JSON is to represent Binary JSON (BSON) 类型简单 JSON。

一般语法是将对象表示为单个嵌入式文档。例如,BSON binary object is represented as a document {"$binary": "<binary data>"}. The $ prefix in the key field indicates the type. The same goes for BSON date object.

方法bson.ParseExtJSONArray() expects extended JSON types to be documents, and not in MongoDB dot-notation表达式。例如,而不是下面的:

{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }

该方法需要:

{ "$match": { "dateTimeGMT": {"$date":"2018-08-22T09:29:25.000Z" } } }

您还可以在 Unix Epoch 中提供日期值,例如:

{ "$match": { "dateTimeGMT": {"$date": { "$numberLong": "1546300800"} } } }

使用 mongo-go-driver/bson,例如:

raw := `[ { "$match": {"dateTimeGMT": {"$date": {"$numberLong": "1530962624753" } } } } ]`
pipeline, err := bson.ParseExtJSONArray(raw)
cursor, err := collection.Aggregate(context.Background(), pipeline)

补充说明: 您可以调试 ParseExtJSONArray(),然后通过迭代将结果值传递给聚合。例如:

toConvert := `[
   { "$lookup": {
        "from": "anotherCollection",
        "localField": "foreignKey",
        "foreignField": "_id",
        "as": "someField"
    }},
    { "$match": {"dateTimeGMT":{"$lt": {"$date": "2019-01-10T09:29:25.000Z" } } } }
]`
pipeline, err := bson.ParseExtJSONArray(toConvert)

it, err := bson.NewArrayIterator(pipeline)
for it.Next() {
    fmt.Println(it.Value().MutableDocument().ToExtJSON(true))
}

//Outputs : 
//   {"$lookup":{"from":"anotherCollection","localField":"foreignKey","foreignField":"_id","as":"someField"}}
//   {"$match":{"dateTimeGMT":{"$lt":{"$date":{"$numberLong":"1547112565000"}}}}}
//