From d7d051e19764b1158c0be29d43c249a81fef0801 Mon Sep 17 00:00:00 2001 From: julius Date: Thu, 28 Apr 2022 03:10:36 -0700 Subject: [PATCH 1/3] initial commit --- pymongo/collection.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/pymongo/collection.py b/pymongo/collection.py index 79b745d355..531eb19c99 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -951,7 +951,22 @@ def update_one( {'x': 4, '_id': 0} {'x': 1, '_id': 1} {'x': 1, '_id': 2} - + + If ``upsert=True`` and no documents match the filter, create a + new document based on the filter criteria and update modifications. + + >>> result = db.test.update_one({'x': -1*2**32}, {'$inc': {'x': 3}}, upsert=True) + >>> result.matched_count + 0 + >>> result.modified_count + 0 + >>> result.upserted_id + ObjectId('626a678eeaa80587d4bb3fb7') + >>> for doc in db.test.find(): + ... print(doc) + ... + {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -4294967293} + :Parameters: - `filter`: A query that matches the document to update. - `update`: The modifications to apply. From 77f79d1570df525d27b46e8ec9c947ad309943ef Mon Sep 17 00:00:00 2001 From: julius Date: Thu, 28 Apr 2022 22:12:39 -0700 Subject: [PATCH 2/3] change magic number to -10 --- pymongo/collection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pymongo/collection.py b/pymongo/collection.py index 0b13231fdd..f9fe1da6ac 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -955,7 +955,7 @@ def update_one( If ``upsert=True`` and no documents match the filter, create a new document based on the filter criteria and update modifications. - >>> result = db.test.update_one({'x': -1*2**32}, {'$inc': {'x': 3}}, upsert=True) + >>> result = db.test.update_one({'x': -10}, {'$inc': {'x': 3}}, upsert=True) >>> result.matched_count 0 >>> result.modified_count @@ -965,7 +965,7 @@ def update_one( >>> for doc in db.test.find(): ... print(doc) ... - {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -4294967293} + {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -7} :Parameters: - `filter`: A query that matches the document to update. From 85752cc028ab41528968893074b1779737f96f07 Mon Sep 17 00:00:00 2001 From: julius Date: Fri, 29 Apr 2022 12:24:03 -0700 Subject: [PATCH 3/3] switch to find_one --- pymongo/collection.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pymongo/collection.py b/pymongo/collection.py index f9fe1da6ac..1d0eb1035e 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -962,9 +962,7 @@ def update_one( 0 >>> result.upserted_id ObjectId('626a678eeaa80587d4bb3fb7') - >>> for doc in db.test.find(): - ... print(doc) - ... + >>> db.test.find_one(result.upserted_id) {'_id': ObjectId('626a678eeaa80587d4bb3fb7'), 'x': -7} :Parameters: