How to parse extended JSON Date in aggregation pipeline using ParseExtJSONArray() in mongo-go-driver

两盒软妹~` 提交于 2020-01-06 02:46:14

问题


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:


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


来源:https://stackoverflow.com/questions/52276568/how-to-parse-extended-json-date-in-aggregation-pipeline-using-parseextjsonarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!