From 7f1939ebcb6f577f762bf5cd068288d2533b2bbf Mon Sep 17 00:00:00 2001 From: Petr Messner Date: Sat, 8 Sep 2018 20:24:57 +0200 Subject: [PATCH] PYTHON-1642 - Replace count() with count_documents({}) in docs (#376) --- bson/binary.py | 8 ++++---- doc/examples/bulk.rst | 2 +- doc/tutorial.rst | 10 +++++----- pymongo/collection.py | 20 ++++++++++---------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/bson/binary.py b/bson/binary.py index 8600125bc..a8bf1ca39 100644 --- a/bson/binary.py +++ b/bson/binary.py @@ -198,9 +198,9 @@ class UUIDLegacy(Binary): ... CodecOptions(uuid_representation=STANDARD)) >>> coll.insert_one({'uuid': Binary(my_uuid.bytes, 3)}).inserted_id ObjectId('...') - >>> coll.find({'uuid': my_uuid}).count() + >>> coll.count_documents({'uuid': my_uuid}) 0 - >>> coll.find({'uuid': UUIDLegacy(my_uuid)}).count() + >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)}) 1 >>> coll.find({'uuid': UUIDLegacy(my_uuid)})[0]['uuid'] UUID('...') @@ -209,9 +209,9 @@ class UUIDLegacy(Binary): >>> doc = coll.find_one({'uuid': UUIDLegacy(my_uuid)}) >>> coll.replace_one({"_id": doc["_id"]}, doc).matched_count 1 - >>> coll.find({'uuid': UUIDLegacy(my_uuid)}).count() + >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)}) 0 - >>> coll.find({'uuid': {'$in': [UUIDLegacy(my_uuid), my_uuid]}}).count() + >>> coll.count_documents({'uuid': {'$in': [UUIDLegacy(my_uuid), my_uuid]}}) 1 >>> coll.find_one({'uuid': my_uuid})['uuid'] UUID('...') diff --git a/doc/examples/bulk.rst b/doc/examples/bulk.rst index f9b5a9f21..9a2e55cde 100644 --- a/doc/examples/bulk.rst +++ b/doc/examples/bulk.rst @@ -29,7 +29,7 @@ bulk insert operations. >>> db = pymongo.MongoClient().bulk_example >>> db.test.insert_many([{'i': i} for i in range(10000)]).inserted_ids [...] - >>> db.test.count() + >>> db.test.count_documents({}) 10000 Mixed Bulk Write Operations diff --git a/doc/tutorial.rst b/doc/tutorial.rst index a7fe33bc8..f854a3d5d 100644 --- a/doc/tutorial.rst +++ b/doc/tutorial.rst @@ -333,20 +333,20 @@ author is "Mike": Counting -------- If we just want to know how many documents match a query we can -perform a :meth:`~pymongo.cursor.Cursor.count` operation instead of a -full query. We can get a count of all of the documents in a -collection: +perform a :meth:`~pymongo.collection.Collection.count_documents` operation +instead of a full query. We can get a count of all of the documents +in a collection: .. doctest:: - >>> posts.count() + >>> posts.count_documents({}) 3 or just of those documents that match a specific query: .. doctest:: - >>> posts.find({"author": "Mike"}).count() + >>> posts.count_documents({"author": "Mike"}) 2 Range Queries diff --git a/pymongo/collection.py b/pymongo/collection.py index 9a13bb93c..647177dbd 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -647,7 +647,7 @@ class Collection(common.BaseObject): session=None): """Insert a single document. - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 0 >>> result = db.test.insert_one({'x': 1}) >>> result.inserted_id @@ -697,12 +697,12 @@ class Collection(common.BaseObject): bypass_document_validation=False, session=None): """Insert an iterable of documents. - >>> db.test.count() + >>> db.test.count_documents({}) 0 >>> result = db.test.insert_many([{'x': i} for i in range(2)]) >>> result.inserted_ids [ObjectId('54f113fffba522406c9cc20e'), ObjectId('54f113fffba522406c9cc20f')] - >>> db.test.count() + >>> db.test.count_documents({}) 2 :Parameters: @@ -1156,12 +1156,12 @@ class Collection(common.BaseObject): def delete_one(self, filter, collation=None, session=None): """Delete a single document matching the filter. - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 3 >>> result = db.test.delete_one({'x': 1}) >>> result.deleted_count 1 - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 2 :Parameters: @@ -1194,12 +1194,12 @@ class Collection(common.BaseObject): def delete_many(self, filter, collation=None, session=None): """Delete one or more documents matching the filter. - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 3 >>> result = db.test.delete_many({'x': 1}) >>> result.deleted_count 3 - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 0 :Parameters: @@ -2434,7 +2434,7 @@ class Collection(common.BaseObject): stage and returns a :class:`~pymongo.change_stream.CollectionChangeStream` cursor which iterates over changes on this collection. - + Introduced in MongoDB 3.6. .. code-block:: python @@ -2875,11 +2875,11 @@ class Collection(common.BaseObject): projection=None, sort=None, session=None, **kwargs): """Finds a single document and deletes it, returning the document. - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 2 >>> db.test.find_one_and_delete({'x': 1}) {u'x': 1, u'_id': ObjectId('54f4e12bfba5220aa4d6dee8')} - >>> db.test.count({'x': 1}) + >>> db.test.count_documents({'x': 1}) 1 If multiple documents match *filter*, a *sort* can be applied.