dongyan1841 2019-06-19 05:43
浏览 707


I try to read and write and delete data from a Go application with the official mongodb driver for go (

Here is my struct I want to use:

Contact struct {
    ID               xid.ID `json:"contact_id" bson:"contact_id"`
    SurName          string `json:"surname" bson:"surname"`
    PreName          string `json:"prename" bson:"prename"`
// xid is

I omit code to add to the collection as this is working find.

I can get a list of contacts with a specific contact_id using the following code (abbreviated):

filter := bson.D{}
cursor, err := contactCollection.Find(nil, filter)
for cur.Next(context.TODO()) {

This works and returns the documents. I thought about doing the same for delete or a matched get:

// delete - abbreviated
filter := bson.M{"contact_id": id}
result, _ := contactCollection.DeleteMany(nil, filter)
// result.DeletedCount is always 0, err is nil
if err != nil {
    sendError(c, err) // helper function

c.JSON(200, gin.H{
    "ok":      true,
    "message": fmt.Sprintf("deleted %d patients", result.DeletedCount),
}) // will be called, it is part of a webservice done with gin

// get complete
func Get(c *gin.Context) {
    defer c.Done()

    id := c.Param("id")
    filter := bson.M{"contact_id": id}

    cur, err := contactCollection.Find(nil, filter)
    if err != nil {
        sendError(c, err) // helper function
    } // no error

    contacts := make([]types.Contact, 0)
    for cur.Next(context.TODO()) { // nothing returned
        // create a value into which the single document can be decoded
        var elem types.Contact
        err := cur.Decode(&elem)
        if err != nil {
            sendError(c, err) // helper function
        contacts = append(contacts, elem)

    c.JSON(200, contacts)

Why does the same filter does not work on delete?

Edit: Insert code looks like this:

_, _ = contactCollection.InsertOne(context.TODO(), Contact{
    ID: "abcdefg",
    SurName: "Demo",
    PreName: "on stackoverflow",
  • 写回答

2条回答 默认 最新

  • dps43378 2019-06-19 07:50

    Contact.ID is of type xid.ID, which is a byte array:

    type ID [rawLen]byte

    So the insert code you provided where you use a string literal to specify the value for the ID field would be a compile-time error:

    _, _ = contactCollection.InsertOne(context.TODO(), Contact{
        ID: "abcdefg",
        SurName: "Demo",
        PreName: "on stackoverflow",

    Later in your comments you clarified that the above insert code was just an example, and not how you actually do it. In your real code you unmarshal the contact (or its ID field) from a request.

    xid.ID has its own unmarshaling logic, which might interpret the input data differently, and might result in an ID representing a different string value than your input. ID.UnmarshalJSON() defines how the string ID will be converted to xid.ID:

    func (id *ID) UnmarshalJSON(b []byte) error {
        s := string(b)
        if s == "null" {
            *id = nilID
            return nil
        return id.UnmarshalText(b[1 : len(b)-1])

    As you can see, the first byte is cut off, and ID.UnmarshalText() does even more "magic" on it (check the source if you're interested).

    All-in-all, to avoid such "transformations" happen in the background without your knowledge, use a simple string type for your ID, and do necessary conversions yourself wherever you need to store / transmit your ID.

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



  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?