Skip to content

Commit 4bf975f

Browse files
changed count to count_documents (openedx#23945)
This PR contributes to the elimination of deprecation warnings, specifically the one mentioned above and reported in the Warnings Report: https://build.testeng.edx.org/job/edx-platform-python-pipeline-master/warning_5freport_5fall_2ehtml/ . Changed collection.find(filter).count() to collection.count_documents(filter) in the following file: common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py And collection.find(filter).limit(1).count() to collection.count_documents(filter, limit=1) in the following file: common/lib/xmodule/xmodule/modulestore/mongo/draft.py The method count_documents is part of the collection, not the cursor (find returns a cursor), according to StackOverflow (https://stackoverflow.com/questions/56303331/warning-message-in-pymongo-count-is-deprecated). Because of that after changing count, I removed the calling to the find method and use the filter parameter in count_documents. Also, I removed limit because count_documents accepts limit parameter. This warning occurs due to deprecation: https://pymongo.readthedocs.io/en/3.9.0/api/pymongo/collection.html#pymongo.collection.Collection.count_documents
1 parent d27ac50 commit 4bf975f

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

common/lib/xmodule/xmodule/modulestore/mongo/draft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def clone_course(self, source_course_id, dest_course_id, user_id, fields=None, *
196196
# b/c we don't want the payload, I'm copying the guts of get_items here
197197
query = self._course_key_to_son(dest_course_id)
198198
query['_id.category'] = {'$nin': ['course', 'about']}
199-
if self.collection.find(query).limit(1).count() > 0:
199+
if self.collection.count_documents(query, limit=1) > 0:
200200
raise DuplicateCourseError(
201201
dest_course_id,
202202
"Course at destination {0} is not an empty course. "

common/lib/xmodule/xmodule/modulestore/tests/test_split_modulestore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,12 +2235,12 @@ def test_schema(self):
22352235
db_connection = modulestore().db_connection
22362236
for collection in [db_connection.course_index, db_connection.structures, db_connection.definitions]:
22372237
self.assertEqual(
2238-
collection.find({'schema_version': {'$exists': False}}).count(),
2238+
collection.count_documents({'schema_version': {'$exists': False}}),
22392239
0,
22402240
"{0.name} has records without schema_version".format(collection)
22412241
)
22422242
self.assertEqual(
2243-
collection.find({'schema_version': {'$ne': SplitMongoModuleStore.SCHEMA_VERSION}}).count(),
2243+
collection.count_documents({'schema_version': {'$ne': SplitMongoModuleStore.SCHEMA_VERSION}}),
22442244
0,
22452245
"{0.name} has records with wrong schema_version".format(collection)
22462246
)

0 commit comments

Comments
 (0)