11//! Models for collection-level batch operations.
22use super :: options:: WriteModel ;
33
4- use bson:: Document ;
4+ use bson:: { Bson , Document } ;
55use std:: convert:: From ;
66
77#[ derive( Debug ) ]
@@ -10,33 +10,54 @@ pub struct DeleteModel {
1010 pub multi : bool ,
1111}
1212
13+ impl DeleteModel {
14+ pub fn new ( filter : Document , multi : bool ) -> DeleteModel {
15+ DeleteModel {
16+ filter : filter,
17+ multi : multi,
18+ }
19+ }
20+ }
21+
1322#[ derive( Debug ) ]
1423pub struct UpdateModel {
1524 pub filter : Document ,
1625 pub update : Document ,
17- pub upsert : bool ,
26+ pub upsert : Option < bool > ,
1827 pub multi : bool ,
19- pub is_replace : bool ,
2028}
2129
2230impl 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 {
2436 UpdateModel {
2537 filter : filter,
2638 update : update,
2739 upsert : upsert,
2840 multi : multi,
29- is_replace : false ,
3041 }
3142 }
3243}
3344
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 ) ) ;
3958 }
59+
60+ document
4061 }
4162}
4263
@@ -69,7 +90,6 @@ impl From<WriteModel> for Batch {
6990 update: update,
7091 upsert: upsert,
7192 multi: false ,
72- is_replace: true ,
7393 } ] )
7494 }
7595 WriteModel :: UpdateOne { filter, update, upsert } => {
@@ -78,7 +98,6 @@ impl From<WriteModel> for Batch {
7898 update: update,
7999 upsert: upsert,
80100 multi: false ,
81- is_replace: false ,
82101 } ] )
83102 }
84103 WriteModel :: UpdateMany { filter, update, upsert } => {
@@ -87,7 +106,6 @@ impl From<WriteModel> for Batch {
87106 update: update,
88107 upsert: upsert,
89108 multi: true ,
90- is_replace: false ,
91109 } ] )
92110 }
93111 }
@@ -148,7 +166,6 @@ impl Batch {
148166 update : update,
149167 upsert : upsert,
150168 multi : false ,
151- is_replace : true ,
152169 } )
153170 }
154171 WriteModel :: UpdateOne { filter, update, upsert } => {
@@ -157,7 +174,6 @@ impl Batch {
157174 update : update,
158175 upsert : upsert,
159176 multi : false ,
160- is_replace : false ,
161177 } )
162178 }
163179 WriteModel :: UpdateMany { filter, update, upsert } => {
@@ -166,7 +182,6 @@ impl Batch {
166182 update : update,
167183 upsert : upsert,
168184 multi : true ,
169- is_replace : false ,
170185 } )
171186 }
172187 _ => return Some ( model) ,
0 commit comments