dongzhanlian6289
2018-09-11 12:59
浏览 70

如何在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"}}}}}
    //
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报