簡體   English   中英

如何使用 bson 使用 golang 更新 MongoDB 中的特定嵌套結構字段

[英]How can I update and specific nested struct field in MongoDB with golang using bson

這就是我想要實現的目標:

bson.M{constants.MONGO_SET: entity.UserShop{Shop.ID: userShop.Shop.ID}}

而是使用

bson.M{constants.MONGO_SET: entity.UserShop{"shop._id": userShop.Shop.ID}}

存在

type UserShop struct {
  User `json:"-"  bson:"-"`
  Shop `json:"shop,omitempty"  bson:"shop,omitempty"`
}

和購物

type Shop struct {
    ID          primitive.ObjectID `json:"-" bson:"_id,omitempty"`
    Name        string             `json:"name,omitempty" bson:"name,omitempty"`
    Description string             `json:"description,omitempty" bson:"description,omitempty"`
    ImageURL    string             `json:"imageURL,omitempty" bson:"imageURL,omitempty"`
    Stars       int64              `json:"stars,omitempty" bson:"stars,omitempty"`
    // Location    *[]int64           `json:"location,omitempty" bson:"location,omitmepty"`
    // Products    *AllProducts       `json:"products,omitempty" bson:"products,omitempty"`
}

在數據庫結構中:

店鋪:{ _id: "12344", name: "hello" }

我想修改任何特定字段並且不要修改 shop 對象內的任何其他字段

使用以下表達式僅更新shop._id字段:

bson.M{"$set":bson.M{"shop._id":userShop.Shop.ID}}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM