如何在mongo-go-driver

时间:2018-09-11 12:59:59

标签: mongodb go aggregation-framework mongo-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" } }
])

有见识吗?

1 个答案:

答案 0 :(得分:2)

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

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

方法bson.ParseExtJSONArray()期望扩展的JSON类型为文档,而不是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"}}}}}
//