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 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能