英文:
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
的):
- 未找到记录,字符串未添加到MongoDB中:
&{0 0 0 <nil>}
- 找到记录,字符串已添加到数据库中的数组中:
&{1 1 0 <nil>}
- 找到记录,字符串已存在于数据库中且未添加:
&{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("ill-formed date format; must be 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("no document matched id")
}
return 1, nil
}
Here are the three scenarios I am seeing and their respective return values (all of which are of type *mongo.UpdateResult
):
- Record not found, string not added to MongoDB:
&{0 0 0 <nil>}
- Record found, string added to the array in the db:
&{1 1 0 <nil>}
- Record found, string is already in db and not added:
&{1 0 0 <nil>}
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
type UpdateResult struct {
MatchedCount int64 // 通过筛选条件匹配的文档数量。
ModifiedCount int64 // 操作修改的文档数量。
UpsertedCount int64 // 操作插入的文档数量。
UpsertedID interface{} // 插入文档的 _id 字段,如果没有插入操作则为 nil。
}
-
记录未找到,字符串未添加到 MongoDB:
&{0 0 0 <nil>}
&mongo.UpdateResult{MatchedCount:0, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}
-
记录找到,字符串添加到数据库中的数组:
&{1 1 0 <nil>}
&mongo.UpdateResult{MatchedCount:1, ModifiedCount:1, UpsertedCount:0, UpsertedID:interface {}(nil)}
-
记录找到,字符串已经存在于数据库中,不进行添加:
&{1 0 0 <nil>}
&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.
}
-
Record not found, string not added to MongoDB:
&{0 0 0 <nil>}
&mongo.UpdateResult{MatchedCount:0, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}
-
Record found, string added to the array in the db:
&{1 1 0 <nil>}
&mongo.UpdateResult{MatchedCount:1, ModifiedCount:1, UpsertedCount:0, UpsertedID:interface {}(nil)}
-
Record found, string is already in db and not added:
&{1 0 0 <nil>}
&mongo.UpdateResult{MatchedCount:1, ModifiedCount:0, UpsertedCount:0, UpsertedID:interface {}(nil)}
The output is printed with fmt.Printf("%#v\n", result)
, see https://pkg.go.dev/fmt.
%#v a Go-syntax representation of the value
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论