MongoDB Compass Filter 表达式转 bson.M 表达式
MongoDB Compass Filter expression to Go bson.M expression
我在 MongoDB Compass 过滤器上有一个过滤器 在两个日期和一个包含两个可能值的字符串之间,如以下代码:
{closed_at: {$gt: ISODate('2020-07-01T00:00:00.700+00:00'),$lt: ISODate('2020-07-30T00:00:00.700+00:00')}, status: { $in: ["paid", "delivered"] }}
(如果在 Go 上过滤相同的值,我希望得到相同的 1256 个文档)
现在我需要将此过滤器转换为有效的 bson.M 表达式,找不到“status”字符串字段的技巧,有此查询表达式但有错误消息:
query := bson.M{
"status" : ["paid", "delivered"], //Error: Invalid array bound '"paid"', the value must be representable by 'int' type
"closed_at": bson.M{"$gt": from, "$lt": to},
}
cursor, err := client.Database("orders").Collection("orders").Find(ctx,query)
¿声明 status 字段 并将值 query 传递给 Find 方法的正确方法是什么?
您没有完全翻译查询:
"status": bson.M{"$in":[]string{"paid","delivered"}}
我在 MongoDB Compass 过滤器上有一个过滤器 在两个日期和一个包含两个可能值的字符串之间,如以下代码:
{closed_at: {$gt: ISODate('2020-07-01T00:00:00.700+00:00'),$lt: ISODate('2020-07-30T00:00:00.700+00:00')}, status: { $in: ["paid", "delivered"] }}
现在我需要将此过滤器转换为有效的 bson.M 表达式,找不到“status”字符串字段的技巧,有此查询表达式但有错误消息:
query := bson.M{
"status" : ["paid", "delivered"], //Error: Invalid array bound '"paid"', the value must be representable by 'int' type
"closed_at": bson.M{"$gt": from, "$lt": to},
}
cursor, err := client.Database("orders").Collection("orders").Find(ctx,query)
¿声明 status 字段 并将值 query 传递给 Find 方法的正确方法是什么?
您没有完全翻译查询:
"status": bson.M{"$in":[]string{"paid","delivered"}}