From 089dcb8ced7d4f913e9f899331c19c6a8f16d7d6 Mon Sep 17 00:00:00 2001 From: Eric Hare Date: Fri, 9 Feb 2024 12:22:27 -0700 Subject: [PATCH] Fix #203 : Implementation for update_many --- astrapy/db.py | 21 +++++++++++++++++++++ tests/astrapy/test_db_dml.py | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/astrapy/db.py b/astrapy/db.py index 26456a19..2de983d2 100644 --- a/astrapy/db.py +++ b/astrapy/db.py @@ -841,6 +841,27 @@ def update_one( return response + def update_many( + self, filter: Dict[str, Any], update: Dict[str, Any] + ) -> API_RESPONSE: + """ + Updates multiple documents in the collection. + Args: + filter (dict): Criteria to identify the document to update. + update (dict): The update to apply to the document. + Returns: + dict: The response from the database after the update operation. + """ + json_query = make_payload(top_level="updateMany", filter=filter, update=update) + + response = self._request( + method=http_methods.POST, + path=f"{self.base_path}", + json_data=json_query, + ) + + return response + def replace(self, path: str, document: API_DOC) -> API_RESPONSE: """ Replace a document in the collection. diff --git a/tests/astrapy/test_db_dml.py b/tests/astrapy/test_db_dml.py index 9a82a402..7f8c5cde 100644 --- a/tests/astrapy/test_db_dml.py +++ b/tests/astrapy/test_db_dml.py @@ -790,6 +790,26 @@ def test_update_one_create_subdocument_novector( assert response["data"]["document"]["name"] == "Eric" +@pytest.mark.describe("update_many to create a subdocument, not through vector") +def test_update_many_create_subdocument_novector( + writable_v_collection: AstraDBCollection, +) -> None: + _id1 = str(uuid.uuid4()) + _id2 = str(uuid.uuid4()) + writable_v_collection.insert_one({"_id": _id1, "name": "Not Eric!"}) + writable_v_collection.insert_one({"_id": _id2, "name": "Not Eric!"}) + update_many_response = writable_v_collection.update_many( + filter={"name": "Not Eric!"}, + update={"$set": {"name": "Eric"}}, + ) + + assert update_many_response["status"]["matchedCount"] > 1 + assert update_many_response["status"]["modifiedCount"] > 1 + + response = writable_v_collection.find(filter={"name": "Eric"}) + assert len(response["data"]["documents"]) > 1 + + @pytest.mark.describe("delete_subdocument, not through vector") def test_delete_subdocument_novector( writable_v_collection: AstraDBCollection,