MongoDB Compass Filter expression to Go bson.M expression

南楼画角 提交于 2021-02-11 12:35:46

问题


I have a filter on MongoDB Compass filter Between two dates and an string of two posible values like this code:

{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"] }}

(I expect the same 1256 Documents if filter the same values on Go)

Now I need to convert this filter to a valid bson.M expression, can´t find the trick for the "status" string filed, have this query expression but have an error message:

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)

¿Which is the correct way to declare the status field and pass value query to Find method?


回答1:


You didn't translate the query completely:

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


来源:https://stackoverflow.com/questions/63347906/mongodb-compass-filter-expression-to-go-bson-m-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!