dsfsdf7852 2018-03-15 07:18 采纳率: 100%
浏览 16
已采纳

无法从Mongo记录[mgo]:golang在结构接口上仅向上插入一个值

Basically I want update in one value from the mongodb document by given fully struct interface as a change parameter in collection.Upsert(selector,change). how we do this without lose other values into empty. Other(type,category.rerportby,createon,info) values should be keep on existing values only update plant and location values into PLANT07 and BAR)

NOTE: I want use completely Service Notification Struct Object for do this.

DatabaseName:WO 
CollectionName:SERVICE_NOTIFICATIONS

package models

//models.ServiceNotification
type ServiceNotification struct {
    NotificationNo string                `json:"notification_no" bson:"notification_no"`
    Type           string                `json:"type" bson:"type"`
    Category       string                `json:"category" bson:"category"`
        Plant          string                `json:"plant" bson:"plant"`
    Location         string              `json:"location" bson:"location"`
    ReportedBy     string                `json:"reportedby" bson:"reportedby"`
    Info           map[string]interface{}`json:"info" bson:"info"`
    SAPInfo        SAPNotificationInfo   `json:"sapinfo" bson:"sapinfo"`
        CreateOn       string                `json:"createon" bson:"createon"`
    UpdateOn       string                `json:"updateon" bson:"updateon"`
}

package main

func main(){

input := models.ServiceNotification{
    NotificationNo:000120,
    Plant:"Plant07",
    Location:"BAR",
}
Change_ServiceNotification(input)

}

I want update plant and location by given complete struct interface to the mongo Upsert function. because I want to decide dynamically what should update . But when I update plant and location other values going to be LOST. in mongo record.

func Change_ServiceNotification(notification models.ServiceNotification) error {
    session, err := commons.GetMongoSession()
    if err != nil {
        return errors.New("Cannot create mongodb session" + err.Error())
    }
    defer session.Close()
    var col = session.DB(WO).C(SERVICE_NOTIFICATIONS)

    selector := bson.M{"notification_no": notification.NotificationNo}

    _, err = col.Upsert(selector, notification)
    if err != nil {
        errMsg := "Cannot update service notification " + err.Error()
        return errors.New(errMsg)
    }
    return nil
}

Appreciate your help

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongleiwei2182 2018-03-15 08:45
    关注

    You cannot do it this way, but you can use the $set operator of MongoDB (Skipping error checking):

    input := Bson.M{
        "$set": bson.M{
            "plant": "Plant07",
            // Further fields here...
        }
    }
    selector := bson.M{"notification_no": notification.NotificationNo}
    col.Upsert(selector, input)
    

    This will update only the provided fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀