Skip to content

Commit ac4bacb

Browse files
authored
PYTHON-1323 Removed Collection.group (#559)
1 parent 7c1060c commit ac4bacb

File tree

8 files changed

+12
-188
lines changed

8 files changed

+12
-188
lines changed

doc/api/pymongo/collection.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,4 @@
6969
.. automethod:: inline_map_reduce
7070
.. automethod:: initialize_unordered_bulk_op
7171
.. automethod:: initialize_ordered_bulk_op
72-
.. automethod:: group
7372
.. automethod:: count

doc/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Breaking Changes in 4.0
3535
- Removed :meth:`pymongo.collection.Collection.update`.
3636
- Removed :meth:`pymongo.collection.Collection.remove`.
3737
- Removed :meth:`pymongo.collection.Collection.find_and_modify`.
38+
- Removed :meth:`pymongo.collection.Collection.group`.
3839
- Removed :meth:`pymongo.mongo_client.MongoClient.close_cursor`. Use
3940
:meth:`pymongo.cursor.Cursor.close` instead.
4041
- Removed :meth:`pymongo.mongo_client.MongoClient.kill_cursors`.

doc/migrate-to-pymongo4.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,16 @@ Can be changed to this::
237237
replaced_doc = collection.find_one_and_replace({'b': 1}, {'c': 1})
238238
deleted_doc = collection.find_one_and_delete({'c': 1})
239239

240+
Collection.group is removed
241+
...........................
242+
243+
Removed :meth:`pymongo.collection.Collection.group`. This method was
244+
deprecated in PyMongo 3.5. MongoDB 4.2 removed the `group command`_.
245+
Use :meth:`~pymongo.collection.Collection.aggregate` with the ``$group`` stage
246+
instead.
247+
248+
.. _group command: https://docs.mongodb.com/manual/reference/command/group/
249+
240250
Collection.ensure_index is removed
241251
..................................
242252

pymongo/collection.py

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,46 +2366,6 @@ def watch(self, pipeline=None, full_document=None, resume_after=None,
23662366
batch_size, collation, start_at_operation_time, session,
23672367
start_after)
23682368

2369-
def group(self, key, condition, initial, reduce, finalize=None, **kwargs):
2370-
"""Perform a query similar to an SQL *group by* operation.
2371-
2372-
**DEPRECATED** - The group command was deprecated in MongoDB 3.4. The
2373-
:meth:`~group` method is deprecated and will be removed in PyMongo 4.0.
2374-
Use :meth:`~aggregate` with the `$group` stage or :meth:`~map_reduce`
2375-
instead.
2376-
2377-
.. versionchanged:: 3.5
2378-
Deprecated the group method.
2379-
.. versionchanged:: 3.4
2380-
Added the `collation` option.
2381-
.. versionchanged:: 2.2
2382-
Removed deprecated argument: command
2383-
"""
2384-
warnings.warn("The group method is deprecated and will be removed in "
2385-
"PyMongo 4.0. Use the aggregate method with the $group "
2386-
"stage or the map_reduce method instead.",
2387-
DeprecationWarning, stacklevel=2)
2388-
group = {}
2389-
if isinstance(key, str):
2390-
group["$keyf"] = Code(key)
2391-
elif key is not None:
2392-
group = {"key": helpers._fields_list_to_dict(key, "key")}
2393-
group["ns"] = self.__name
2394-
group["$reduce"] = Code(reduce)
2395-
group["cond"] = condition
2396-
group["initial"] = initial
2397-
if finalize is not None:
2398-
group["finalize"] = Code(finalize)
2399-
2400-
cmd = SON([("group", group)])
2401-
collation = validate_collation_or_none(kwargs.pop('collation', None))
2402-
cmd.update(kwargs)
2403-
2404-
with self._socket_for_reads(session=None) as (sock_info, slave_ok):
2405-
return self._command(sock_info, cmd, slave_ok,
2406-
collation=collation,
2407-
user_fields={'retval': 1})["retval"]
2408-
24092369
def rename(self, new_name, session=None, **kwargs):
24102370
"""Rename this collection.
24112371

test/test_collation.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,6 @@ def test_explain_command(self):
188188
self.collation.document,
189189
self.last_command_started()['explain']['collation'])
190190

191-
@raisesConfigurationErrorForOldMongoDB
192-
@client_context.require_version_max(4, 1, 0, -1)
193-
def test_group(self):
194-
self.db.test.group('foo', {'foo': {'$gt': 42}}, {},
195-
'function(a, b) { return a; }',
196-
collation=self.collation)
197-
self.assertCollationInLastCommand()
198-
199191
@raisesConfigurationErrorForOldMongoDB
200192
def test_map_reduce(self):
201193
self.db.test.map_reduce('function() {}', 'function() {}', 'output',

test/test_custom_types.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -588,19 +588,6 @@ def run_test(doc_cls):
588588
for doc_cls in [RawBSONDocument, OrderedDict]:
589589
run_test(doc_cls)
590590

591-
@client_context.require_version_max(4, 1, 0, -1)
592-
def test_group_w_custom_type(self):
593-
db = self.db
594-
test = db.get_collection('test', codec_options=UNINT_CODECOPTS)
595-
test.insert_many([
596-
{'sku': 'a', 'qty': UndecipherableInt64Type(2)},
597-
{'sku': 'b', 'qty': UndecipherableInt64Type(5)},
598-
{'sku': 'a', 'qty': UndecipherableInt64Type(1)}])
599-
600-
self.assertEqual([{'sku': 'b', 'qty': UndecipherableInt64Type(5)},],
601-
test.group(["sku", "qty"], {"sku": "b"}, {},
602-
"function (obj, prev) { }"))
603-
604591
def test_aggregate_w_custom_type_decoder(self):
605592
db = self.db
606593
db.test.insert_many([

test/test_legacy_api.py

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -74,124 +74,6 @@ def setUpClass(cls):
7474
def tearDownClass(cls):
7575
cls.deprecation_filter.stop()
7676

77-
@client_context.require_version_max(4, 1, 0, -1)
78-
def test_group(self):
79-
db = self.db
80-
db.drop_collection("test")
81-
82-
self.assertEqual([],
83-
db.test.group([], {}, {"count": 0},
84-
"function (obj, prev) { prev.count++; }"
85-
))
86-
87-
db.test.insert_many([{"a": 2}, {"b": 5}, {"a": 1}])
88-
89-
self.assertEqual([{"count": 3}],
90-
db.test.group([], {}, {"count": 0},
91-
"function (obj, prev) { prev.count++; }"
92-
))
93-
94-
self.assertEqual([{"count": 1}],
95-
db.test.group([], {"a": {"$gt": 1}}, {"count": 0},
96-
"function (obj, prev) { prev.count++; }"
97-
))
98-
99-
db.test.insert_one({"a": 2, "b": 3})
100-
101-
self.assertEqual([{"a": 2, "count": 2},
102-
{"a": None, "count": 1},
103-
{"a": 1, "count": 1}],
104-
db.test.group(["a"], {}, {"count": 0},
105-
"function (obj, prev) { prev.count++; }"
106-
))
107-
108-
# modifying finalize
109-
self.assertEqual([{"a": 2, "count": 3},
110-
{"a": None, "count": 2},
111-
{"a": 1, "count": 2}],
112-
db.test.group(["a"], {}, {"count": 0},
113-
"function (obj, prev) "
114-
"{ prev.count++; }",
115-
"function (obj) { obj.count++; }"))
116-
117-
# returning finalize
118-
self.assertEqual([2, 1, 1],
119-
db.test.group(["a"], {}, {"count": 0},
120-
"function (obj, prev) "
121-
"{ prev.count++; }",
122-
"function (obj) { return obj.count; }"))
123-
124-
# keyf
125-
self.assertEqual([2, 2],
126-
db.test.group("function (obj) { if (obj.a == 2) "
127-
"{ return {a: true} }; "
128-
"return {b: true}; }", {}, {"count": 0},
129-
"function (obj, prev) "
130-
"{ prev.count++; }",
131-
"function (obj) { return obj.count; }"))
132-
133-
# no key
134-
self.assertEqual([{"count": 4}],
135-
db.test.group(None, {}, {"count": 0},
136-
"function (obj, prev) { prev.count++; }"
137-
))
138-
139-
self.assertRaises(OperationFailure, db.test.group,
140-
[], {}, {}, "5 ++ 5")
141-
142-
@client_context.require_version_max(4, 1, 0, -1)
143-
def test_group_with_scope(self):
144-
db = self.db
145-
db.drop_collection("test")
146-
db.test.insert_many([{"a": 1}, {"b": 1}])
147-
148-
reduce_function = "function (obj, prev) { prev.count += inc_value; }"
149-
150-
self.assertEqual(2, db.test.group([], {}, {"count": 0},
151-
Code(reduce_function,
152-
{"inc_value": 1}))[0]['count'])
153-
self.assertEqual(4, db.test.group([], {}, {"count": 0},
154-
Code(reduce_function,
155-
{"inc_value": 2}))[0]['count'])
156-
157-
self.assertEqual(1,
158-
db.test.group([], {}, {"count": 0},
159-
Code(reduce_function,
160-
{"inc_value": 0.5}))[0]['count'])
161-
162-
self.assertEqual(2, db.test.group(
163-
[], {}, {"count": 0},
164-
Code(reduce_function, {"inc_value": 1}))[0]['count'])
165-
166-
self.assertEqual(4, db.test.group(
167-
[], {}, {"count": 0},
168-
Code(reduce_function, {"inc_value": 2}))[0]['count'])
169-
170-
self.assertEqual(1, db.test.group(
171-
[], {}, {"count": 0},
172-
Code(reduce_function, {"inc_value": 0.5}))[0]['count'])
173-
174-
@client_context.require_version_max(4, 1, 0, -1)
175-
def test_group_uuid_representation(self):
176-
db = self.db
177-
coll = db.uuid
178-
coll.drop()
179-
uu = uuid.uuid4()
180-
coll.insert_one({"_id": uu, "a": 2})
181-
coll.insert_one({"_id": uuid.uuid4(), "a": 1})
182-
183-
reduce = "function (obj, prev) { prev.count++; }"
184-
coll = self.db.get_collection(
185-
"uuid", CodecOptions(uuid_representation=STANDARD))
186-
self.assertEqual([],
187-
coll.group([], {"_id": uu},
188-
{"count": 0}, reduce))
189-
coll = self.db.get_collection(
190-
"uuid", CodecOptions(uuid_representation=PYTHON_LEGACY))
191-
self.assertEqual([{"count": 1}],
192-
coll.group([], {"_id": uu},
193-
{"count": 0}, reduce))
194-
19577

19678
class TestLegacyBulk(BulkTestBase):
19779

test/test_read_preferences.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def setUpClass(cls):
360360
if client_context.auth_enabled:
361361
cls.c.admin.authenticate(db_user, db_pwd)
362362
cls.client_version = Version.from_client(cls.c)
363-
# mapReduce and group fail with no collection
363+
# mapReduce fails if the collection does not exist.
364364
coll = cls.c.pymongo_test.get_collection(
365365
'test', write_concern=WriteConcern(w=client_context.w))
366366
coll.insert_one({})
@@ -431,13 +431,6 @@ def test_create_collection(self):
431431
lambda: self.c.pymongo_test.create_collection(
432432
'some_collection%s' % random.randint(0, sys.maxsize)))
433433

434-
@client_context.require_version_max(4, 1, 0, -1)
435-
def test_group(self):
436-
with warnings.catch_warnings():
437-
warnings.simplefilter("ignore")
438-
self._test_coll_helper(True, self.c.pymongo_test.test, 'group',
439-
{'a': 1}, {}, {}, 'function() { }')
440-
441434
def test_map_reduce(self):
442435
self._test_coll_helper(False, self.c.pymongo_test.test, 'map_reduce',
443436
'function() { }', 'function() { }',

0 commit comments

Comments
 (0)