【发布时间】:2021-05-26 23:44:09
【问题描述】:
我正在尝试编写一个查询,该查询给出创建日期等于 365 的文档。 我在 mongo 指南针中尝试了聚合,但不知道如何在 Go 中翻译它。以下是我在 mongo compass 中尝试过的内容
[{$project: {fileName:1,fileType:1,fileData:1,subtractDate: {$eq:[{$trunc:{$divide:[{$subtract:[newDate(),"$creationDate"]},1000*60*60*24]}},365]}}}]
由此我创建了一个视图并将过滤器应用为 - {subtractDate:true}
下面是我的 golang 代码,请帮我看看我做错了什么,因为错误来了 - missing ',' before newline in composite literal syntax
matchStage := bson.D{{"$match", bson.D{{}}}}
groupStage := bson.D{{"$project": bson.D{{"fileName": 1,"fileType": 1,"fileData": 1,"subtractDate":bson.D{{"$trunc": bson.D{{"$divide": []bson.D{{"$subtract": []time.Time{time.Date(),"$creationDate"}},1000 * 60 * 60 * 24}}}}}}}}}
myCollection := client.Database("dbname").Collection("collectionName")
filterCursor2,err = myCollection.Aggregate(ctx,mongo.Pipeline{matchStage,groupStage})
【问题讨论】:
标签: mongodb go aggregation