问题
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