相关疑难解决方法(0)

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

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

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

go mongodb aggregation-framework mongo-go

1
推荐指数
1
解决办法
2256
查看次数

标签 统计

aggregation-framework ×1

go ×1

mongo-go ×1

mongodb ×1