【问题标题】:Golang and MongoDB: DeleteMany with filterGolang 和 MongoDB:带过滤器的 DeleteMany
【发布时间】:2019-11-01 18:48:41
【问题描述】:

我尝试使用官方的 mongodb 驱动程序 (go.mongodb.org/mongo-driver) 从 Go 应用程序读取、写入和删除数据。

这是我要使用的结构:

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 https://github.com/rs/xid

我省略了添加到集合中的代码,因为这是查找。

我可以使用以下代码(缩写)获取具有特定contact_id的联系人列表:

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

这有效并返回文档。我想过对删除或匹配的获取做同样的事情:

// 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
    return
}

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
        return
    } // 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
            return
        }
        contacts = append(contacts, elem)
    }

    c.JSON(200, contacts)
}

为什么同一个过滤器对删除不起作用?

编辑:插入代码如下:

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

【问题讨论】:

  • 什么是xid.ID?您可以发布类型声明及其方法吗?还有,DeleteMany() 返回错误,不要省略,错误是什么?可能是您无权删除文档...
  • 我省略了错误,因为它没有返回错误
  • 另外你必须使用bson标签,而不是json。请在Contact 结构中将json 更改为bson,或者如果您也使用JSON,也请添加bson
  • @icza 更新了更多代码
  • 看起来你用字符串idFind(),和DeleteMany()一样吗?另外,你试过bson标签吗?

标签: mongodb go mongo-go


【解决方案1】:

Contact.IDxid.ID 类型,是一个字节数组:

type ID [rawLen]byte

因此,您提供的插入代码使用string 文字来指定ID 字段的值将是编译时错误:

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

稍后在您的 cmets 中,您澄清了上述插入代码只是一个示例,而不是您实际执行的方式。在您的真实代码中,您可以从请求中解组联系人(或其 ID 字段)。

xid.ID 有自己的解组逻辑,它可能会以不同的方式解释输入数据,并可能导致 ID 代表与您的输入不同的 string 值。 ID.UnmarshalJSON() 定义了string ID 将如何转换为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])
}

如您所见,第一个字节被截断,ID.UnmarshalText() 对它做了更多的“魔术”(如果您有兴趣,请查看源代码)。

总而言之,为避免在您不知情的情况下在后台发生此类“转换”,请为您的 ID 使用简单的string 类型,并在需要存储/传输您的 ID 的任何地方自行进行必要的转换。

【讨论】:

    【解决方案2】:

    对于 ID 字段,您应该使用 bson 包提供的primitive.ObjectID

    "go.mongodb.org/mongo-driver/bson/primitive"
    
    ID          primitive.ObjectID `json:"_id" bson:"_id"`
    

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 2019-03-04
      相关资源
      最近更新 更多