1

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

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

有什么见解吗?

4

1 回答 1

3

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

一般语法是将对象表示为单个嵌入文档。例如,BSON二进制对象表示为一个文档{"$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"}}}}}
//
于 2018-09-12T00:05:22.050 回答