Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pysnippets/database/mongodb/mongo_document_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def update_document(collection, filter_criteria, update_data, multiple=False):
dict: The update result details.
"""
if multiple:
result = collection.update_many(filter_criteria, {'$set': update_data})
result = collection.update_many(filter_criteria, update_data)
else:
result = collection.update_one(filter_criteria, {'$set': update_data})
result = collection.update_one(filter_criteria, update_data)
Comment on lines +15 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== repo files matching target =="
fd -a 'mongo_document_update.py|database.md|test_mongo.py' . || true

echo "== target snippet and surrounding context =="
if [ -f pysnippets/database/mongodb/mongo_document_update.py ]; then
  wc -l pysnippets/database/mongodb/mongo_document_update.py
  cat -n pysnippets/database/mongodb/mongo_document_update.py
fi

echo "== doc references =="
if [ -f pysnippets/database/database.md ]; then
  nl -ba pysnippets/database/database.md | sed -n '1,220p'
fi

echo "== test references =="
if [ -f Tests/database/test_mongo.py ]; then
  wc -l Tests/database/test_mongo.py
  nl -ba Tests/database/test_mongo.py | sed -n '1,260p'
fi

echo "== search update_many/update_one usages in repo =="
rg -n "update_many|update_one|mongo_document_update|update_data|set.*age|age.*31" . || true

Repository: UTSAVS26/PySnippets

Length of output: 1324


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== database.md =="
if [ -f pysnippets/database/database.md ]; then
  awk '{printf "%6d\t%s\n", NR, $0}' pysnippets/database/database.md | sed -n '1,240p'
fi

echo "== test_mongo.py =="
if [ -f Tests/database/test_mongo.py ]; then
  awk '{printf "%6d\t%s\n", NR, $0}' Tests/database/test_mongo.py | sed -n '1,280p'
fi

echo "== relevant repository usages =="
rg -n "update_many|update_one|update_document|update_data|set|age.*31|\$set" . || true

echo "== repository commit diff stat/name-only =="
git diff --stat HEAD~1..HEAD 2>/dev/null || git diff --stat 2>/dev/null || true
git diff --name-only HEAD~1..HEAD 2>/dev/html|sed -n '1,200p' || true

Repository: UTSAVS26/PySnippets

Length of output: 50376


🌐 Web query:

PyMongo collection update_one update document operator required behavior passing field update updateData

💡 Result:

In PyMongo, the update_one() method requires an update document that specifies the modifications to apply to the matched document [1][2]. This update document must use MongoDB update operator expressions (e.g., $set, $inc, $unset) to modify specific fields [1][3][4]. Key Requirements and Behavior: 1. Update Operators: The update parameter must be a mapping (dictionary) that contains update operator expressions [1][2]. For example, to update a field named "status" to "active", you must pass {'$set': {'status': 'active'}} rather than simply passing {'status': 'active'} [3]. Passing a naked document (without operators) as the update parameter is generally incorrect for update_one() and will typically cause an error or unexpected behavior, as MongoDB will interpret it as an attempt to replace the entire document content if used in a replacement context, which update_one() does not support [3]. 2. Update Document Structure: - The update document must be a dictionary-like object [2]. - It specifies the change to perform using operators like $set (to modify a field), $inc (to increment a numeric field), or $unset (to remove a field) [4][5]. 3. Alternative (Aggregation Pipeline): Starting in MongoDB 4.2 and supported by PyMongo, the update parameter can also be an aggregation pipeline (a list of stages) instead of an update operator document [6][3][2]. This allows for more complex updates, such as setting a field based on the value of another field [3]. Example of standard usage: collection.update_one({'_id': 1}, {'$set': {'field_to_update': 'new_value'}}) Summary: Always wrap your field updates in appropriate operators like $set within the update dictionary to ensure correct modification of the targeted document [1][3].

Citations:


Preserve the automatic $set wrapping or update the API contract.

update_data is still documented as plain field data ({"age": 31}), and Tests/database/test_mongo.py expects $set wrapping. Passing those values directly to PyMongo makes this a document replacement-style update contract rather than a targeted field update. Either wrap update_data with $set again when callers provide field data, or change all caller/test expectations to full MongoDB update documents.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pysnippets/database/mongodb/mongo_document_update.py` around lines 15 - 17,
Restore automatic $set wrapping for update_data in the update_many and
update_one branches so callers can continue passing plain field data such as
{"age": 31}. Preserve the existing filter_criteria and multi-update selection
behavior; do not switch the API contract to require full MongoDB update
documents.

return result.raw_result
7 changes: 4 additions & 3 deletions pysnippets/database/mysql/sql_connection_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import psycopg2
import mysql.connector

def connect_to_mysql(host, user, password, database):
"""
Connects to a MySQL database.
Expand All @@ -14,6 +11,8 @@ def connect_to_mysql(host, user, password, database):
Returns:
mysql.connector.connection.MySQLConnection: Connection object if successful.
"""
import mysql.connector

connection = mysql.connector.connect(
host=host,
user=user,
Expand All @@ -35,6 +34,8 @@ def connect_to_postgresql(host, user, password, database):
Returns:
psycopg2.extensions.connection: Connection object if successful.
"""
import psycopg2

connection = psycopg2.connect(
host=host,
user=user,
Expand Down