Skip to content

Commit 0c57ea0

Browse files
committed
[FEATURE] Import and delete methods of MongoDB optimized.
1 parent d2b142f commit 0c57ea0

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

src/infrastructure/repository/mongo_db/e_action.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[derive(PartialEq)]
12
pub(crate) enum EAction {
23
FIND,
34
DELETE,

src/infrastructure/repository/mongo_db/mongo_db_repository.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,23 +158,32 @@ impl MongoDbRepository {
158158
let collection = self.collection(&query.data_base(), &query.collection());
159159

160160
let mut cursor = self.find_cursor(query).await?;
161+
162+
let mut ids_to_action = vec![];
161163

162164
while let Some(r_document) = cursor.next().await {
163-
if r_document.is_err() {
164-
let exception = ConnectException::new(r_document.unwrap_err().to_string());
165+
if let Err(error) = r_document {
166+
let exception = ConnectException::new(error.to_string());
165167
return Err(exception);
166168
}
167-
169+
168170
let document = r_document.unwrap();
171+
172+
if let Some(id) = document.get("_id") {
173+
ids_to_action.push(id.clone());
174+
}
175+
169176
let data = self.make_document_data(query.data_base(), query.collection(), &document)?;
170177
documents.push(data);
171-
172-
match action {
173-
EAction::FIND => (),
174-
EAction::DELETE => self.delete_document(&collection, &document).await?,
175-
EAction::UPDATE => self.update_document(&collection, &document, value).await?,
178+
179+
if action == EAction::UPDATE {
180+
self.update_document(&collection, &document, value).await?;
176181
}
177182
}
183+
184+
if action == EAction::DELETE {
185+
self.delete_document(&collection, ids_to_action).await?;
186+
}
178187

179188
let r_total = collection.estimated_document_count(None).await;
180189
if let Err(error) = r_total {
@@ -200,8 +209,8 @@ impl MongoDbRepository {
200209

201210
fn make_document_data(&self, data_base: String, collection: String, document: &Document) -> Result<DocumentData, ConnectException> {
202211
let json = serde_json::to_string(&document);
203-
if json.is_err() {
204-
let exception = ConnectException::new(json.unwrap_err().to_string());
212+
if let Err(error) = json {
213+
let exception = ConnectException::new(error.to_string());
205214
return Err(exception);
206215
}
207216

@@ -218,12 +227,15 @@ impl MongoDbRepository {
218227
))
219228
}
220229

221-
async fn delete_document(&self, collection: &Collection<Document>, document: &Document) -> Result<(), ConnectException> {
222-
let result = collection.delete_one(document.clone(), None).await;
230+
async fn delete_document(&self, collection: &Collection<Document>, id_documents: Vec<Bson>) -> Result<(), ConnectException> {
231+
let delete_filter = doc! { "_id": { "$in": id_documents } };
232+
233+
let result = collection.delete_many(delete_filter, None).await;
223234
if result.is_err() {
224235
let exception = ConnectException::new(result.unwrap_err().to_string());
225236
return Err(exception);
226237
}
238+
227239
Ok(())
228240
}
229241

@@ -417,10 +429,18 @@ impl IDBRepository for MongoDbRepository {
417429
}
418430

419431
async fn collection_import(&self, query: &CollectionQuery, documents: Vec<String>) -> Result<String, ConnectException> {
432+
let collection = self.collection(&query.data_base(), &query.collection());
433+
434+
let mut parsed = Vec::new();
420435
for document in documents {
421-
let fix = DocumentQuery::from(query.data_base(), query.collection(), None, None, None);
422-
self.insert(query, &document).await?;
436+
parsed.push(self.document_from_string(&document)?);
423437
}
438+
439+
if let Err(error) = collection.insert_many(parsed, None).await {
440+
let exception = ConnectException::new(error.to_string());
441+
return Err(exception);
442+
}
443+
424444
Ok(String::new())
425445
}
426446

0 commit comments

Comments
 (0)