第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何在Golang中運(yùn)行MongoDB查詢,并$cond

如何在Golang中運(yùn)行MongoDB查詢,并$cond

Go
江戶川亂折騰 2022-09-05 10:06:20
我嘗試在Golang中運(yùn)行MongoDB查詢并收到一些錯誤。如果字段“section.gateBack”等于0,我想用新的時間戳更新字段“expiresAt”。數(shù)據(jù)模型如下所示:{  "_id": "78b0e7de-c24c-11eb-8529-0242ac130003",  "expiresAt": "2021-04-22T14:06:55.069Z",  "section": {    "gateFront": 1,    "gateMiddle": 1,    "gateBack": 0  }}在第一次嘗試中,我編寫了查詢,就像我執(zhí)行其他(簡單)$set操作一樣,但這次使用$cond使用if-else:filter := bson.M{    "_id": iD,}update := bson.M{    "$set": bson.M{        "expiresAt": bson.M{            "$cond": bson.M{                "if": bson.M{                    "$and": []bson.M{                        {"section.gateBack": bson.M{"$eq": 0}},                        {"section.gateBack": bson.M{"$exists": true}},                    },                },                "then": time.Now().AddDate(0, 1, 0),                "else": time.Now().AddDate(0, 0, 1),            },        },    },}res, err := s.coll.UpdateOne(    ctx,    filter,    update,)但是使用此更新操作,我得到以下錯誤:multiple write errors: [{write errors: [{The dollar ($) prefixed field '$cond' in 'expiresAt.$cond' is not valid for storage.}]}, {<nil>}]我在MongoDB開發(fā)人員方面發(fā)現(xiàn)了一個問題,它說更新值必須是一個數(shù)組:https://developer.mongodb.com/community/forums/t/mongoerror-the-dollar-prefixed-field-cond-in-energy-cond-is-not-valid-for-storage/16448當(dāng)我閱讀MongoDB的官方文檔時,這是有道理的,用于使用聚合管道的UpdateOne():https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#example-2因此,我更改了我的更新值并將其附加到 bson 數(shù)組中。M:filter := bson.M{    "_id": iD,}var pipeline []bson.Mupdate := bson.M{    "$set": bson.M{        "expiresAt": bson.M{            "$cond": bson.M{                "if": bson.M{                    "$and": []bson.M{                        {"section.gateBack": bson.M{"$eq": 0}},                        {"section.gateBack": bson.M{"$exists": true}},                    },                },                "then": time.Now().AddDate(0, 1, 0),                "else": time.Now().AddDate(0, 0, 1),            },        },    },}pipeline = append(pipeline, update)res, err := s.coll.UpdateOne(    ctx,    filter,    pipeline,)
查看完整描述

2 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個贊

在轉(zhuǎn)換查詢時,使用映射和切片類型并查找類型來代替 mongo 驅(qū)動程序別名。bson.A{bson.M{...}, bson.M{...}}[]bson.M{...}bson.M{}bson.A{}



查看完整回答
反對 回復(fù) 2022-09-05
?
湖上湖

TA貢獻(xiàn)2003條經(jīng)驗(yàn) 獲得超2個贊

很抱歉回復(fù)晚了。我完全忘記了發(fā)布解決方案。就像@prasad_說的,我試圖使用開關(guān)盒運(yùn)算符,但使用.UpdateOne()


ctx := context.Background()


filter := bson.M{

    "_id": ID,

}


back := bson.M{

    "case": bson.M{

        "$and": []bson.M{

            {"$gt": []interface{}{"$section.gateBack", 0}},

            {"$exists": []interface{}{"$section.gateBack", true}},

        },

    },

    "then": time.Now().AddDate(0, 0, 1),

}


middle := bson.M{

    "case": bson.M{

        "$and": []bson.M{

            {"$gt": []interface{}{"$section.gateMiddle", 0}},

            {"$exists": []interface{}{"$section.gateMiddle", true}},

        },

    },

    "then": time.Now().AddDate(0, 1, 0),

}


front := bson.M{

    "case": bson.M{

        "$and": []bson.M{

            {"$gt": []interface{}{"$section.gateFront", 0}},

            {"$exists": []interface{}{"$section.gateFront", true}},

            {"$in": []interface{}{"$section.access", []string{"gate", "door"}}},

        },

    },

    "then": time.Now().AddDate(1, 0, 0),

}


cases := bson.A{back, middle, front}


update := []bson.M{

    {

        "$set": bson.M{

            "expiresAt": bson.M{

                "$switch": bson.M{

                    "branches": cases,

                    "default":  time.Now().AddDate(0, 0, 0),

                },

            },

        },

    },

}


res, err := coll.UpdateOne(

    ctx,

    filter,

    update,

)

if err != nil {

    return 0, err

}


查看完整回答
反對 回復(fù) 2022-09-05
  • 2 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號