我需要从订单那里获取所有文件:status == 'paid' AND status == 'delivered' ANDclosed_at BETWEEN startDate AND endDate ;我已经使用以下代码访问了我的 mongoDB 连接:package mainimport ( "context" "fmt" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/readpref" "time")func main() { mx, err := time.LoadLocation("America/Mexico_City") endDate := time.Now().In(mx) startDate := endDate.AddDate(0, 0, -1) if err != nil { fmt.Println(err); return } client, err := mongo.NewClient(options.Client().ApplyURI("mongodb+srv://<userName>:<pass>@someDomain/orders")) if err != nil { fmt.Println(err); return } ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) err = client.Connect(ctx) if err != nil { fmt.Println(err); return } defer client.Disconnect(ctx) err = client.Ping(ctx, readpref.Primary()) if err != nil { fmt.Println(err); return } orders := client.Database("orders").Collection("orders") //here I want to filter with the status 'paid' and 'delivered' and closerd_at between dates.}func Bod(t time.Time) time.Time { year, month, day := t.Date() return time.Date(year, month, day, 0, 0, 0, 0, t.Location())}我怀疑的另一件事是,在文档上,closed_at日期格式如下:2020-04-23T21:28:46.642+00:00我from和to日期的格式如下:startDate: 2020-08-06 00:00:00 -0500 CDTendDate: 2020-08-07 00:00:00 -0500 CDT
1 回答
米琪卡哇伊
TA贡献1998条经验 获得超6个赞
这是我的解决方案:
query := bson.M{
"status": bson.M{"$in":[]string{"paid","delivered"}}, //only status paid and delivered
"closed_at": bson.M{"$gt": from, "$lt": to}, //between dates
}
cursor, err := client.Database("orders").Collection("orders").Find(ctx,query)
- 1 回答
- 0 关注
- 109 浏览
添加回答
举报
0/150
提交
取消