-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.go
More file actions
50 lines (43 loc) · 1.48 KB
/
Copy pathdelete.go
File metadata and controls
50 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package morm
import "context"
// Delete deletes a single document from the specified collection based on the provided filter.
//
// Parameters:
// - filter: The filter to match documents for deletion.
// - ctx: Optional context.Context for the delete operation. If not provided, the default context will be used.
//
// Returns:
// - error: An error if any occurred during the delete operation.
func (qb *CollectQueryBuilder) Delete(filter interface{}, ctx ...context.Context) error {
collection := qb.c.collection
var backgroundContext = context.Background()
if len(ctx) > 0 {
backgroundContext = ctx[0]
}
_, err := collection.DeleteOne(backgroundContext, filter)
if err != nil {
return err
}
return nil
}
// DeleteMany deletes multiple documents from the specified collection based on the provided filter.
//
// Parameters:
// - filter: The filter to match documents for deletion.
// - ctx: Optional context.Context for the delete operation. If not provided, the default context will be used.
//
// Returns:
// - int64: The number of documents deleted.
// - error: An error if any occurred during the delete operation.
func (qb *CollectQueryBuilder) DeleteMany(filter interface{}, ctx ...context.Context) (int64, error) {
collection := qb.c.collection
var backgroundContext = context.Background()
if len(ctx) > 0 {
backgroundContext = ctx[0]
}
result, err := collection.DeleteMany(backgroundContext, filter)
if err != nil {
return 0, err
}
return result.DeletedCount, nil
}