如何在mongo-go-driver中使用ParseExtJSONArray()在聚合管道中解析扩展的JSON日期 [英] How to parse extended JSON Date in aggregation pipeline using ParseExtJSONArray() in mongo-go-driver

查看:121
本文介绍了如何在mongo-go-driver中使用ParseExtJSONArray()在聚合管道中解析扩展的JSON日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期"字段的收藏集:

I've got a collection with a Date field:

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

我正在尝试使用mongo-go-driver的ParseExtJSONArray函数在$ match聚合阶段按日期查找. (我知道如何直接使用* bson.Array进行此操作.我要问的是,我知道用ParserExtJSONArray进行处理的正确方法,或者是否遇到了限制.)

I'm trying to find by date in a $match aggregation stage with the ParseExtJSONArray function of mongo-go-driver. (I am aware of how to do this with *bson.Array directly. I'm asking so I know the right way to do it with ParserExtJSONArray or if I've run up against a limitation.)

我已简化为该示例,并确认它与上述文档不匹配.

I've simplified to this example and confirmed it is failing to match the above document.

pipeline, err := bson.ParseExtJSONArray(`[
    { "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)

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

The following doesn't work in the mongo shell. ( Not surprising because it converts automatically to ISODate() format )

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

但这确实在mongo shell中起作用.

But this does work in the mongo shell.

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

但是这会在管道"中返回一个空数组. (因为ParseExtJSONArray无法处理JavaScript)

But this returns an empty array in "pipeline". (Because ParseExtJSONArray doesn't handle JavaScript)

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

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

Because it then uses an empty array it retuns all the documents in the collection. Interestingly, the date is formatted differently in the document we are trying to match.

{
    "_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中不起作用.

And this doesn't work in the mongo shell.

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

有见识吗?

推荐答案

The idea behind MongoDB Extended JSON is to represent Binary JSON (BSON) types in plain JSON.

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

The general syntax is to represent an object as a single embedded document. For example, BSON binary object is represented as a document {"$binary": "<binary data>"}. The $ prefix in the key field indicates the type. The same goes for BSON date object.

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

The method bson.ParseExtJSONArray() expects extended JSON types to be documents, and not in MongoDB dot-notation expression. For example, instead of below:

{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }

该方法预期:

{ "$match": { "dateTimeGMT": {"$date":"2018-08-22T09:29:25.000Z" } } }

您还可以在 Unix时代中提供日期值,例如:

You can also supply the date value in Unix Epoch, for example:

{ "$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()之前将结果值传递给聚合之前进行调试.例如:

Extra Note: you can debug ParseExtJSONArray() before passing the resulting value to aggregation by iterating over it. For example:

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"}}}}}
//

这篇关于如何在mongo-go-driver中使用ParseExtJSONArray()在聚合管道中解析扩展的JSON日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆