【发布时间】:2021-04-28 17:13:08
【问题描述】:
我正在尝试使用 mongo-driver 在 Golang 中执行以下 Mongo 查询:
var currentDate = ISODate("2021-01-22T00:00:00Z")
var someValue = "A"
db.someCollection.aggregate([
{$match: {"data.date": currentDate, someAttribute: someValue}},
{$project: {
data: {$filter: {
input: '$data',
as: 'df',
cond: {$eq: ['$$df.date', currentDate]}
}},
_id: 0
}}
])
我尝试了以下方法,但运气不佳:
currentDate := time.Date(2021, 1, 22, 0, 0, 0, 0, time.UTC)
someValue := "A"
matchStage := bson.D{{"$match", bson.D{{"data.date", currentDate}, {"someAttribute", someValue}}}}
projectStage := bson.D{{"$project", bson.D{{"data", bson.D{{"$filter", bson.D{{"input", "'$data'"}, {"as", "df"}, {"cond", bson.D{{"eq", bson.A{"$$df.date", currentDate}}}}}}}}}}}
cur, err := someCollection.Aggregate(a.ctx, mongo.Pipeline{matchStage, projectStage})
出现以下错误:input to $filter must be an array not string
我该如何解决这个问题?
【问题讨论】: