亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取 MongoDB 文檔 WHERE 狀態和日期之間

獲取 MongoDB 文檔 WHERE 狀態和日期之間

Go
慕少森 2022-06-21 16:38:22
我需要從訂單那里獲取所有文件: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)


查看完整回答
反對 回復 2022-06-21
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號