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

And*_*man 1 go mongodb aggregation-framework mongo-go

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

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

我试图使用 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)
Run Code Online (Sandbox Code Playgroud)

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

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

但这在 mongo shell 中确实有效。

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

但这会在“pipeline”中返回一个空数组。(因为 ParseExtJSONArray 不处理 JavaScript)

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

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

{
    "_id" : { "$oid" : "5b92b359ddceef5b24502834" },
    "dateTimeGMT" : { "$date" : "2018-08-22T05:29:25-04:00" },
    yada yada yada
}
Run Code Online (Sandbox Code Playgroud)

但这也不匹配。

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

这在 mongo shell 中不起作用。

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

有什么见解吗?

Wan*_*iar 5

MongoDB 扩展 JSON背后的想法是用纯 JSON 表示二进制 JSON ( BSON ) 类型。

一般语法是将对象表示为单个嵌入文档。例如,BSON二进制对象表示为文档{"$binary": "<binary data>"}。键字段中的前缀$指示类型。BSON日期对象也是如此。

方法bson.ParseExtJSONArray()期望扩展 JSON 类型是文档,而不是 MongoDB点表示法表达式。例如,而不是下面的:

{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
Run Code Online (Sandbox Code Playgroud)

该方法期望:

{ "$match": { "dateTimeGMT": {"$date":"2018-08-22T09:29:25.000Z" } } }
Run Code Online (Sandbox Code Playgroud)

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

{ "$match": { "dateTimeGMT": {"$date": { "$numberLong": "1546300800"} } } }
Run Code Online (Sandbox Code Playgroud)

使用mongo-go-driver/bson,一个例子是:

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

额外注意: 您可以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"}}}}}
//
Run Code Online (Sandbox Code Playgroud)