卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35187本站已运行394

检查对象数组中的值是否存在 golang

检查对象数组中的值是否存在 golang

php小编百草在本文将为您介绍如何在golang中检查对象数组中的值是否存在。在开发过程中,我们经常需要对数组进行操作和判断,而在某些情况下,我们需要检查某个值是否存在于一个对象数组中。这个过程可能会涉及到遍历数组、比较值等操作,下面我们将一步步为您介绍如何实现这个功能。

问题内容

我试图在向 mongo 数据库附加新值之前检查该值是否存在,但每次都会收到错误。

obid, _ := primitive.objectidfromhex(id)
        query := bson.d{{key: "_id", value: obid}}
    
        var result bson.m
        er := r.collection.findone(ctx, bson.m{"_id": obid, "statusdata.status": bson.m{"$in": []string{string(p.status)}}}).decode(&result)
        if er != nil {
            if er == mongo.errnodocuments {
                return nil, errors.new(fmt.sprintf("err na  %v, %v", er.error(), p.status))
            }
            return nil, errors.new(fmt.sprintf("err norr  %v", er.error()))
        }

doc, err := utils.todoc(p)
    if err != nil {
        return nil, errors.new(err.error())
    }

    update := bson.d{{key: "$set", value: doc}}
    res := r.collection.findoneandupdate(ctx, query, update, options.findoneandupdate().setreturndocument(1))

我的文档看起来像这样

{
  "statusdata": [
                {
                    "status": "new",
                    "message": "you created a new dispatch request",
                    "createdat": "1657337212751",
                    "updatedat": null
                },
                {
                    "status": "assigned",
                    "message": "justin has been assigned to you",
                    "createdat": "1657412029130",
                    "updatedat": null,
                    "_id": "62ca19bdf7d864001cabfa4a"
                }
            ],
            "createdat": "2022-07-10t00:09:01.785z",

.... }

有不同的状态,我想确保在使用新值更新数据库之前不会多次将相同的状态发送到数据库。

type statustype string

const (
    new              statustype = "new"
    acknowledged     statustype = "acknowledged"
    assigned         statustype = "assigned"
    reject           statustype = "rejected"
    cancel           statustype = "cancelled"
    complete         statustype = "completed"
)

utils.todoc

func todoc(v interface{}) (doc *bson.d, err error) {
    data, err := bson.marshal(v)
    if err != nil {
        return
    }

    err = bson.unmarshal(data, &doc)
    return
}

尝试更新

filter := bson.m{
        "_id":               obid,
        "statusdata.status": bson.m{"$ne": p.status},
    }
    update := bson.m{
        "$push": bson.m{
            "statusdata": newstatustoadd,
        },
        "$set": bson.d{{key: "$set", value: doc}},
    }

    result, err := r.collection.updateone(ctx, filter, update)
    if err != nil {
        // handle error
        return nil, errors.new(err.error())
    }
    if result.matchedcount == 0 {
        // the status already exists in statusdata
    } else if result.modifiedcount == 1 {
        // new status was added successfuly
    }

返回错误


"write exception: write errors: [The dollar ($) prefixed field '$set'
in '$set' is not allowed in the context of an update's replacement
document. Consider using an aggregation pipeline with $replaceWith.]"

解决方法

使用过滤器来排除具有您要添加的状态的文档。如果状态已存在于数组中,则此过滤器将不匹配任何文档。仅当状态尚未添加时才会执行更新操作:

var newStatusToAdd = ... // This is the new statusData document you want to add

filter := bson.M{
    "_id": obId,
    "statusData.status": bson.M{"$ne": p.Status},
}
update := bson.M{
    "$push": bson.M{
        "statusData": newStatusToAdd,
    },
    "$set": doc,
}

result, err := r.collection.UpdateOne(ctx, filter, update)
if err != nil {
    // Handle error
    return
}
if result.MatchedCount == 0 {
    // The status already exists in statusData
} else if result.ModifiedCount == 1 {
    // new status was added successfuly
}
卓越飞翔博客
上一篇: 使用 Python 将点分隔值转换为 Go 结构
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏