dongzhanlian6289 2018-09-11 12:59
浏览 72
已采纳

如何在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
}

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)

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

But this does work in the mongo shell.

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

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
}

But this doesn't match either.

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

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

Any insight?

  • 写回答

1条回答 默认 最新

  • dongyuli4538 2018-09-12 00:05
    关注

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

    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.

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

    The method expects:

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

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

    { "$match": { "dateTimeGMT": {"$date": { "$numberLong": "1546300800"} } } }
    

    Using mongo-go-driver/bson, an example would be:

    raw := `[ { "$match": {"dateTimeGMT": {"$date": {"$numberLong": "1530962624753" } } } } ]`
    pipeline, err := bson.ParseExtJSONArray(raw)
    cursor, err := collection.Aggregate(context.Background(), pipeline)
    

    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"}}}}}
    //
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染