小编典典

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

go

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

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

但这会在“管道”中返回一个空数组。(因为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" } }
])

有见识吗?


阅读 280

收藏
2020-07-02

共1个答案

小编典典

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

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

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

{ "$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"}}}}}
//
2020-07-02