1
1
//! Models for collection-level batch operations.
2
2
use super :: options:: WriteModel ;
3
3
4
- use bson:: Document ;
4
+ use bson:: { Bson , Document } ;
5
5
use std:: convert:: From ;
6
6
7
7
#[ derive( Debug ) ]
@@ -10,33 +10,54 @@ pub struct DeleteModel {
10
10
pub multi : bool ,
11
11
}
12
12
13
+ impl DeleteModel {
14
+ pub fn new ( filter : Document , multi : bool ) -> DeleteModel {
15
+ DeleteModel {
16
+ filter : filter,
17
+ multi : multi,
18
+ }
19
+ }
20
+ }
21
+
13
22
#[ derive( Debug ) ]
14
23
pub struct UpdateModel {
15
24
pub filter : Document ,
16
25
pub update : Document ,
17
- pub upsert : bool ,
26
+ pub upsert : Option < bool > ,
18
27
pub multi : bool ,
19
- pub is_replace : bool ,
20
28
}
21
29
22
30
impl UpdateModel {
23
- pub fn new ( filter : Document , update : Document , upsert : bool , multi : bool ) -> UpdateModel {
31
+ pub fn new ( filter : Document ,
32
+ update : Document ,
33
+ upsert : Option < bool > ,
34
+ multi : bool )
35
+ -> UpdateModel {
24
36
UpdateModel {
25
37
filter : filter,
26
38
update : update,
27
39
upsert : upsert,
28
40
multi : multi,
29
- is_replace : false ,
30
41
}
31
42
}
32
43
}
33
44
34
- impl DeleteModel {
35
- pub fn new ( filter : Document , multi : bool ) -> DeleteModel {
36
- DeleteModel {
37
- filter : filter,
38
- multi : multi,
45
+ impl From < UpdateModel > for Document {
46
+ fn from ( model : UpdateModel ) -> Self {
47
+ let mut document = doc ! {
48
+ "q" => ( model. filter) ,
49
+ "u" => ( model. update)
50
+ } ;
51
+
52
+ if let Some ( upsert) = model. upsert {
53
+ document. insert ( "upsert" , Bson :: Boolean ( upsert) ) ;
54
+ }
55
+
56
+ if model. multi {
57
+ document. insert ( "multi" , Bson :: Boolean ( true ) ) ;
39
58
}
59
+
60
+ document
40
61
}
41
62
}
42
63
@@ -69,7 +90,6 @@ impl From<WriteModel> for Batch {
69
90
update: update,
70
91
upsert: upsert,
71
92
multi: false ,
72
- is_replace: true ,
73
93
} ] )
74
94
}
75
95
WriteModel :: UpdateOne { filter, update, upsert } => {
@@ -78,7 +98,6 @@ impl From<WriteModel> for Batch {
78
98
update: update,
79
99
upsert: upsert,
80
100
multi: false ,
81
- is_replace: false ,
82
101
} ] )
83
102
}
84
103
WriteModel :: UpdateMany { filter, update, upsert } => {
@@ -87,7 +106,6 @@ impl From<WriteModel> for Batch {
87
106
update: update,
88
107
upsert: upsert,
89
108
multi: true ,
90
- is_replace: false ,
91
109
} ] )
92
110
}
93
111
}
@@ -148,7 +166,6 @@ impl Batch {
148
166
update : update,
149
167
upsert : upsert,
150
168
multi : false ,
151
- is_replace : true ,
152
169
} )
153
170
}
154
171
WriteModel :: UpdateOne { filter, update, upsert } => {
@@ -157,7 +174,6 @@ impl Batch {
157
174
update : update,
158
175
upsert : upsert,
159
176
multi : false ,
160
- is_replace : false ,
161
177
} )
162
178
}
163
179
WriteModel :: UpdateMany { filter, update, upsert } => {
@@ -166,7 +182,6 @@ impl Batch {
166
182
update : update,
167
183
upsert : upsert,
168
184
multi : true ,
169
- is_replace : false ,
170
185
} )
171
186
}
172
187
_ => return Some ( model) ,
0 commit comments