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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序