Skip to content

Commit

Permalink
Fix #203 : Implementation for update_many
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare committed Feb 9, 2024
1 parent 9c0e47e commit 089dcb8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions tests/astrapy/test_db_dml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 089dcb8

Please sign in to comment.