diff options
author | Mike Crute <mike@crute.us> | 2021-11-15 23:06:13 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2021-11-15 23:06:13 -0800 |
commit | ffabe67e36537fc4758e00c69ce564837012421d (patch) | |
tree | 4618c759f1d951ce06bb21e9ef4a8ca33d0988aa /db | |
parent | 276e4e6765646b04efaa6784604f11d468ee0bb2 (diff) | |
download | golib-ffabe67e36537fc4758e00c69ce564837012421d.tar.bz2 golib-ffabe67e36537fc4758e00c69ce564837012421d.tar.xz golib-ffabe67e36537fc4758e00c69ce564837012421d.zip |
db: Add DeleteOneById
Diffstat (limited to 'db')
-rw-r--r-- | db/mongodb/client.go | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/db/mongodb/client.go b/db/mongodb/client.go index 98369b9..82082fc 100644 --- a/db/mongodb/client.go +++ b/db/mongodb/client.go | |||
@@ -2,6 +2,7 @@ package mongodb | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "fmt" | ||
5 | "net/url" | 6 | "net/url" |
6 | 7 | ||
7 | "go.mongodb.org/mongo-driver/bson" | 8 | "go.mongodb.org/mongo-driver/bson" |
@@ -88,3 +89,16 @@ func (m *Mongo) ReplaceOneById(ctx context.Context, cn string, id string, in int | |||
88 | 89 | ||
89 | return nil | 90 | return nil |
90 | } | 91 | } |
92 | |||
93 | func (m *Mongo) DeleteOneById(ctx context.Context, cn string, id string) error { | ||
94 | dr, err := m.db.Collection(cn).DeleteOne(ctx, bson.M{"_id": id}) | ||
95 | if err != nil { | ||
96 | return err | ||
97 | } | ||
98 | |||
99 | if dr.DeletedCount != 1 { | ||
100 | return fmt.Errorf("Invalid deletion record count %d not 1", dr.DeletedCount) | ||
101 | } | ||
102 | |||
103 | return nil | ||
104 | } | ||