我有一个带有日期字段的集合:
{
"_id" : ObjectId("5b92b359ddceef5b24502834"),
"dateTimeGMT" : ISODate("2018-08-22T09:29:25.000Z"),
yada, yada, yada
}
我正在尝试使用 mongo-go-driver 的 ParseExtJSONArray 函数在 $match 聚合阶段按日期查找。(我知道如何直接使用 *bson.Array 执行此操作。我在问,所以我知道使用 ParserExtJSONArray 执行此操作的正确方法,或者我是否遇到了限制。)
我已经简化了这个例子,并确认它与上面的文档不匹配。
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
]`)
cursor, err := receivedFromResponseQueue.Aggregate(ctx, pipeline)
以下在 mongo shell 中不起作用。(不足为奇,因为它会自动转换为 ISODate() 格式)
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT.$date":"2018-08-22T09:29:25.000Z" } }
])
但这确实适用于 mongo shell。
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT": ISODate("2018-08-22T09:29:25.000Z") } }
])
但这会在“管道”中返回一个空数组。(因为 ParseExtJSONArray 不处理 JavaScript)
pipeline, err := bson.ParseExtJSONArray(`[
{ "$match": { "dateTimeGMT":ISODate("2018-08-22T09:29:25.000Z") } }
]`)
因为它随后使用了一个空数组,所以它会重新调整集合中的所有文档。有趣的是,日期在我们试图匹配的文档中的格式不同。
{
"_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 中不起作用。
db.getCollection('received_from_response_queue').aggregate([
{ "$match": { "dateTimeGMT.$date":"2018-08-22T05:29:25-04:00" } }
])
有什么见解吗?