How can I read data from *mongo.UpdateResult type in Golang (UpdateOne, $addToSet)

huangapple go评论69阅读模式
英文:

How can I read data from *mongo.UpdateResult type in Golang (UpdateOne, $addToSet)

问题

我正在使用Go MongoDB驱动程序在MongoDB中查找和更新一条记录,这一切都按预期进行。我在处理collection.UpdateOne的返回值时遇到了困难(类型为*mongo.UpdateResult)。以下是该函数的代码:

func AddPaidByYYYYmm(id string, YYYYmm string) (int, error) {
    ctx := context.Background()
    _id, err := primitive.ObjectIDFromHex(id)
    if err != nil {
        log.Fatal(err)
    }
    if !CheckYearMonthFormat(YYYYmm) {
        return 0, errors.New("日期格式错误;必须为YYYY-mm")
    }
    result, err := collection.UpdateOne(ctx, bson.M{"_id": _id}, bson.M{"$addToSet": bson.M{"paid": YYYYmm}})
    fmt.Println(result)
    if err != nil {
        log.Fatal(err)
    }
    if result.MatchedCount == 0 {
        return 0, errors.New("未找到匹配的文档")
    }
    return 1, nil
}

这里有三种情况及其相应的返回值(都是类型为*mongo.UpdateResult的):

  1. 未找到记录,字符串未添加到MongoDB中:&{0 0 0 <nil>}
  2. 找到记录,字符串已添加到数据库中的数组中:&{1 1 0 <nil>}
  3. 找到记录,字符串已存在于数据库中且未添加:&{1 0 0 <nil>}

问题是,我不知道如何读取这些数据。如果有关如何获取这些数据的任何指导,将非常有帮助。如果我在任何地方表达不清楚或有任何问题,请告诉我。

英文:

I am utilizing the Go MongoDB driver to find and update one record in MongoDB, which works as expected. I am struggling with the return value from collection.UpdateOne (of type *mongo.UpdateResult). Here is the function

func AddPaidByYYYYmm(id string, YYYYmm string) (int, error) {
	ctx := context.Background()
	_id, err := primitive.ObjectIDFromHex(id)
	if err != nil {
		log.Fatal(err)
	}
	if !CheckYearMonthFormat(YYYYmm) {
		return 0, errors.New(&quot;ill-formed date format; must be YYYY-mm&quot;)
	}
	result, err := collection.UpdateOne(ctx, bson.M{&quot;_id&quot;: _id}, bson.M{&quot;$addToSet&quot;: bson.M{&quot;paid&quot;: YYYYmm}})
	fmt.Println(result)
	if err != nil {
		log.Fatal(err)
	}
	if result.MatchedCount == 0 {
		return 0, errors.New(&quot;no document matched id&quot;)
	}
	return 1, nil
}

Here are the three scenarios I am seeing and their respective return values (all of which are of type *mongo.UpdateResult):

  1. Record not found, string not added to MongoDB: &amp;{0 0 0 &lt;nil&gt;}
  2. Record found, string added to the array in the db: &amp;{1 1 0 &lt;nil&gt;}
  3. Record found, string is already in db and not added: &amp;{1 0 0 &lt;nil&gt;}

The issue is, I do not know how to read this data. Any guidance on how to get to this would be helpful. Please let me know if I have been unclear on anything or if anyone has any questions.

答案1

得分: 1

请看mongo.UpdateResult文档

type UpdateResult struct {
	MatchedCount  int64       // 通过筛选条件匹配的文档数量。
	ModifiedCount int64       // 操作修改的文档数量。
	UpsertedCount int64       // 操作插入的文档数量。
	UpsertedID    interface{} // 插入文档的 _id 字段,如果没有插入操作则为 nil。
}
  1. 记录未找到,字符串未添加到 MongoDB:&amp;{0 0 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:0, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}

  2. 记录找到,字符串添加到数据库中的数组:&amp;{1 1 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:1, ModifiedCount:1, UpsertedCount:0, UpsertedID:interface {}(nil)}

  3. 记录找到,字符串已经存在于数据库中,不进行添加:&amp;{1 0 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:1, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}

输出结果使用 fmt.Printf("%#v\n", result) 进行打印,参见 https://pkg.go.dev/fmt

%#v	值的 Go 语法表示
英文:

See the doc for mongo.UpdateResult:

type UpdateResult struct {
	MatchedCount  int64       // The number of documents matched by the filter.
	ModifiedCount int64       // The number of documents modified by the operation.
	UpsertedCount int64       // The number of documents upserted by the operation.
	UpsertedID    interface{} // The _id field of the upserted document, or nil if no upsert was done.
}
  1. Record not found, string not added to MongoDB: &amp;{0 0 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:0, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}

  2. Record found, string added to the array in the db: &amp;{1 1 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:1, ModifiedCount:1, UpsertedCount:0, UpsertedID:interface {}(nil)}

  3. Record found, string is already in db and not added: &amp;{1 0 0 &lt;nil&gt;}

    &amp;mongo.UpdateResult{MatchedCount:1, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}

The output is printed with fmt.Printf(&quot;%#v\n&quot;, result), see https://pkg.go.dev/fmt.

%#v	a Go-syntax representation of the value

huangapple
  • 本文由 发表于 2023年5月12日 08:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76232471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定