douquan1015 2014-04-03 19:15
浏览 382
已采纳

Mgo即使不为空也忽略字段

I was wondering if there is any way to have a stuct field that doesn't get commited to mgo even if it isn't empty.

The only way I have found to do this is to make the field lowercase, which makes it a pain to access. Is there another way?

This is an example, and my goal here is to not commit the SSN into the database but still have it uppercase.

package main

import (
  "fmt"
    "crypto/sha1"
    "encoding/base64"
    "labix.org/v2/mgo"
)

type Person struct{
  Name string
  SSN string
  HashedSSN string
}

func main() {
  bob := Person{"Bob", "fake_ssn", ""}
  hasher := sha1.New()
  hasher.Write( []byte(bob.SSN))
  sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
  bob.HashedSSN = sha
  mgoSession, err := mgo.Dial("localhost:27017")
  if err != nil {
    fmt.Println("mongo_config#initMongoSessions : Could not dial to mgoSession", err)
  } else {
    mgoSession.DB("test").C("person").Insert(bob)
  }
}

Thanks,

  • 写回答

1条回答 默认 最新

  • dounue1965 2014-04-03 19:40
    关注

    You can do that by using the field tag as follows:

    type T struct {
        Field string `bson:"-"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端