为了账号安全,请及时绑定邮箱和手机立即绑定

Golang 和 MongoDB:带过滤器的DeleteMany

Golang 和 MongoDB:带过滤器的DeleteMany

Go
慕桂英4014372 2023-06-26 16:40:59
我尝试使用 Go 的官方 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 - abbreviatedfilter := bson.M{"contact_id": id}result, _ := contactCollection.DeleteMany(nil, filter)// result.DeletedCount is always 0, err is nilif 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 completefunc 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",})
查看完整描述

2 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

Contact.ID类型为xid.ID,它是一个字节数组:

type ID [rawLen]byte

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

_, _ = contactCollection.InsertOne(context.TODO(), Contact{

    ID: "abcdefg",

    SurName: "Demo",

    PreName: "on stackoverflow",

})

后来在您的评论中,您澄清了上面的插入代码只是一个示例,而不是您实际的操作方式。在您的真实代码中,您从请求中解组联系人(或其 ID 字段)。

xid.ID有其自己的解组逻辑,这可能会以不同的方式解释输入数据,并可能导致 ID 表示string与您的输入不同的值。ID.UnmarshalJSON()定义stringID 如何转换为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()对其进行了更多“魔法”(如果您感兴趣,请检查源代码)。

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


查看完整回答
反对 回复 2023-06-26
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

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


"go.mongodb.org/mongo-driver/bson/primitive"


ID          primitive.ObjectID `json:"_id" bson:"_id"`


查看完整回答
反对 回复 2023-06-26
  • 2 回答
  • 0 关注
  • 202 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信