@@ -423,6 +423,114 @@ async def delete_documents_async(self, document_ids: List[str]) -> None:
423423 return
424424 await self ._collection_async .delete_many (filter = {"id" : {"$in" : document_ids }})
425425
426+ def delete_by_filter (self , filters : Dict [str , Any ]) -> int :
427+ """
428+ Deletes all documents that match the provided filters.
429+
430+ :param filters: The filters to apply to select documents for deletion.
431+ For filter syntax, see [Haystack metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering)
432+ :returns: The number of documents deleted.
433+ """
434+ self ._ensure_connection_setup ()
435+ assert self ._collection is not None
436+
437+ try :
438+ normalized_filters = _normalize_filters (filters )
439+ result = self ._collection .delete_many (filter = normalized_filters )
440+ deleted_count = result .deleted_count
441+ logger .info (
442+ "Deleted {n_docs} documents from collection '{collection}' using filters." ,
443+ n_docs = deleted_count ,
444+ collection = self .collection_name ,
445+ )
446+ return deleted_count
447+ except Exception as e :
448+ msg = f"Failed to delete documents by filter from MongoDB Atlas: { e !s} "
449+ raise DocumentStoreError (msg ) from e
450+
451+ async def delete_by_filter_async (self , filters : Dict [str , Any ]) -> int :
452+ """
453+ Asynchronously deletes all documents that match the provided filters.
454+
455+ :param filters: The filters to apply to select documents for deletion.
456+ For filter syntax, see [Haystack metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering)
457+ :returns: The number of documents deleted.
458+ """
459+ await self ._ensure_connection_setup_async ()
460+ assert self ._collection_async is not None
461+
462+ try :
463+ normalized_filters = _normalize_filters (filters )
464+ result = await self ._collection_async .delete_many (filter = normalized_filters )
465+ deleted_count = result .deleted_count
466+ logger .info (
467+ "Deleted {n_docs} documents from collection '{collection}' using filters." ,
468+ n_docs = deleted_count ,
469+ collection = self .collection_name ,
470+ )
471+ return deleted_count
472+ except Exception as e :
473+ msg = f"Failed to delete documents by filter from MongoDB Atlas: { e !s} "
474+ raise DocumentStoreError (msg ) from e
475+
476+ def update_by_filter (self , filters : Dict [str , Any ], meta : Dict [str , Any ]) -> int :
477+ """
478+ Updates the metadata of all documents that match the provided filters.
479+
480+ :param filters: The filters to apply to select documents for updating.
481+ For filter syntax, see [Haystack metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering)
482+ :param meta: The metadata fields to update.
483+ :returns: The number of documents updated.
484+ """
485+ self ._ensure_connection_setup ()
486+ assert self ._collection is not None
487+
488+ try :
489+ normalized_filters = _normalize_filters (filters )
490+ # Build update operation to set metadata fields
491+ # MongoDB stores documents with flatten=False, so metadata is in the "meta" field
492+ update_fields = {f"meta.{ key } " : value for key , value in meta .items ()}
493+ result = self ._collection .update_many (filter = normalized_filters , update = {"$set" : update_fields })
494+ updated_count = result .modified_count
495+ logger .info (
496+ "Updated {n_docs} documents in collection '{collection}' using filters." ,
497+ n_docs = updated_count ,
498+ collection = self .collection_name ,
499+ )
500+ return updated_count
501+ except Exception as e :
502+ msg = f"Failed to update documents by filter in MongoDB Atlas: { e !s} "
503+ raise DocumentStoreError (msg ) from e
504+
505+ async def update_by_filter_async (self , filters : Dict [str , Any ], meta : Dict [str , Any ]) -> int :
506+ """
507+ Asynchronously updates the metadata of all documents that match the provided filters.
508+
509+ :param filters: The filters to apply to select documents for updating.
510+ For filter syntax, see [Haystack metadata filtering](https://docs.haystack.deepset.ai/docs/metadata-filtering)
511+ :param meta: The metadata fields to update.
512+ :returns: The number of documents updated.
513+ """
514+ await self ._ensure_connection_setup_async ()
515+ assert self ._collection_async is not None
516+
517+ try :
518+ normalized_filters = _normalize_filters (filters )
519+ # Build update operation to set metadata fields
520+ # MongoDB stores documents with flatten=False, so metadata is in the "meta" field
521+ update_fields = {f"meta.{ key } " : value for key , value in meta .items ()}
522+ result = await self ._collection_async .update_many (filter = normalized_filters , update = {"$set" : update_fields })
523+ updated_count = result .modified_count
524+ logger .info (
525+ "Updated {n_docs} documents in collection '{collection}' using filters." ,
526+ n_docs = updated_count ,
527+ collection = self .collection_name ,
528+ )
529+ return updated_count
530+ except Exception as e :
531+ msg = f"Failed to update documents by filter in MongoDB Atlas: { e !s} "
532+ raise DocumentStoreError (msg ) from e
533+
426534 def delete_all_documents (self , * , recreate_collection : bool = False ) -> None :
427535 """
428536 Deletes all documents in the document store.
0 commit comments