/
builder.go
136 lines (124 loc) · 2.97 KB
/
builder.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package sutando
import (
"context"
"go.mongodb.org/mongo-driver/mongo"
)
type builder struct {
col *mongo.Collection
}
// TODO: custom coder
// withCoder sets custom coder of types/structure.
// func (b builder) withCoder() *builder {
// return &b
// }
// Insert provides the operation to insert documents.
//
// // insert one
// resultOne, _, err := db.Collection("col_name").
// Insert(&obj).
// Exec(ctx)
//
// // insert many
// _, resultMany, err := db.Collection("col_name").
// Insert(&obj1, &obj2, &obj3).
// Exec(ctx)
func (b builder) Insert(p ...any) inserting {
return newInsert(b.col, newBsonEncoder(), p...)
}
// Update provides the operation to update documents with structure. (updates all fields, including empty fields)
//
// // update one
// result, err := db.Collection("col_name").
// UpdateWith(&obj).
// Equal("name", "yanun").
// Exists("age", true).
// First().
// Exec(ctx, true)
//
// // update many
// result, err := db.Collection("col_name").
// UpdateWith(&obj).
// Equal("name", "yanun").
// Exists("age", true).
// Exec(ctx, true)
func (b builder) UpdateWith(p any) updating {
return newUpdate(b.col, newBsonEncoder(), p)
}
// Update provides the operation to update documents with set.
//
// // update one
// result, err := db.Collection("col_name").
// Update().
// Equal("name", "yanun").
// Exists("age", true).
// Set("name", "new_name").
// First().
// Exec(ctx, true)
//
// // update many
// result, err := db.Collection("col_name").
// Update().
// Equal("name", "yanun").
// Exists("age", true).
// Set("name", "new_name").
// Exec(ctx, true)
func (b builder) Update() updating {
return newUpdate(b.col, newBsonEncoder(), nil)
}
// Find provides the operation to find documents.
//
// // find one
// err := db.Collection("col_name").
// Find().
// Equal("name", "yanun").
// Exists("age", true).
// First().
// Exec(ctx, &elem)
//
// // find many
// err := db.Collection("col_name").
// Find().
// Equal("name", "yanun").
// Exists("age", true).
// Exec(ctx, &elems)
func (b builder) Find() finding {
return newFind(b.col)
}
// Delete provides the operation to delete documents.
//
// // delete one
// err := db.Collection("col_name").
// Delete().
// Equal("name", "yanun").
// Exists("age", true).
// First().
// Exec(ctx)
//
// // delete many
// err := db.Collection("col_name").
// Delete().
// Equal("name", "yanun").
// Exists("age", true).
// Exec(ctx)
func (b builder) Delete() deleting {
return newDelete(b.col)
}
// Scalar provides the operation to scalars documents.
//
// // count
// count, err := db.Collection("col_name").
// Scalar().
// Equal("name", "yanun").
// Exists("age", true).
// Count(ctx, "optional_index1", "optional_index2")
func (b builder) Scalar() querying {
return newQuery(b.col)
}
// Drop drops this collection from database.
func (b builder) Drop(ctx context.Context) error {
return b.col.Drop(ctx)
}
// query is for package testing.
func (b builder) query() querying {
return newQuery(b.col)
}