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.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料