MongoDB Compass Filter表达式转换为Go bson.M表达式

问题描述

我在 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"] }}

enter image description here

(如果在Go上过滤相同的值,我希望使用相同的1256个文档)

现在,我需要将此过滤器转换为有效的bson.M表达式,找不到“状态”字符串的窍门,使用此查询表达式,但会收到错误消息:

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)

¿哪种是声明状态字段并将值 query 传递给Find方法的正确方法

解决方法

您没有完全翻译查询:

"status": bson.M{"$in":[]string{"paid","delivered"}}