Repository: MongoEngine/mongoengine

Issue:
Minimum required version of pymongo should probably be bumped to 3.0
`WriteConcern.document` was introduced in pymongo 3.0:

https://github.com/MongoEngine/mongoengine/blob/88c4aa2d87f4479a22aaaef8ec9c697fe94777e1/mongoengine/context_managers.py#L248


Discussion:
Good catch @neuroid , pymongo 3.5 is actually the "oldest" version against which we run the test suite in travis and recent version of pymongo are [backward compatibly](https://docs.mongodb.com/ecosystem/drivers/driver-compatibility-reference/#python-driver-compatibility) so I think it is safe to push it a bit further and require pymongo>=3.5. I don't see any reason why people would want to upgrade mongoengine but stick with an old version of pymongo. I'll push a PR shortly to fix the requirement

Resolving patch:
diff --git a/mongoengine/document.py b/mongoengine/document.py
--- a/mongoengine/document.py
+++ b/mongoengine/document.py
@@ -451,16 +451,6 @@ def _save_create(self, doc, force_insert, write_concern):
 
             object_id = wc_collection.insert_one(doc).inserted_id
 
-        # In PyMongo 3.0, the save() call calls internally the _update() call
-        # but they forget to return the _id value passed back, therefore getting it back here
-        # Correct behaviour in 2.X and in 3.0.1+ versions
-        if not object_id and pymongo.version_tuple == (3, 0):
-            pk_as_mongo_obj = self._fields.get(self._meta['id_field']).to_mongo(self.pk)
-            object_id = (
-                self._qs.filter(pk=pk_as_mongo_obj).first() and
-                self._qs.filter(pk=pk_as_mongo_obj).first().pk
-            )  # TODO doesn't this make 2 queries?
-
         return object_id
 
     def _get_update_doc(self):
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -80,7 +80,7 @@ def get_version(version_tuple):
     long_description=LONG_DESCRIPTION,
     platforms=['any'],
     classifiers=CLASSIFIERS,
-    install_requires=['pymongo>=2.7.1', 'six'],
+    install_requires=['pymongo>=3.5', 'six'],
     test_suite='nose.collector',
     **extra_opts
 )


Test patch:
diff --git a/tests/document/indexes.py b/tests/document/indexes.py
--- a/tests/document/indexes.py
+++ b/tests/document/indexes.py
@@ -565,10 +565,6 @@ class BlogPost(Document):
         self.assertEqual(BlogPost.objects.count(), 10)
         self.assertEqual(BlogPost.objects.hint().count(), 10)
 
-        # PyMongo 3.0 bug only, works correctly with 2.X and 3.0.1+ versions
-        if pymongo.version != '3.0':
-            self.assertEqual(BlogPost.objects.hint([('tags', 1)]).count(), 10)
-
         if MONGO_VER >= MONGODB_32:
             # Mongo32 throws an error if an index exists (i.e `tags` in our case)
             # and you use hint on an index name that does not exist
diff --git a/tests/document/json_serialisation.py b/tests/document/json_serialisation.py
--- a/tests/document/json_serialisation.py
+++ b/tests/document/json_serialisation.py
@@ -61,10 +61,6 @@ def __eq__(self, other):
         self.assertEqual(doc, Doc.from_json(doc.to_json()))
 
     def test_json_complex(self):
-
-        if pymongo.version_tuple[0] <= 2 and pymongo.version_tuple[1] <= 3:
-            raise SkipTest("Need pymongo 2.4 as has a fix for DBRefs")
-
         class EmbeddedDoc(EmbeddedDocument):
             pass
 
diff --git a/tests/queryset/queryset.py b/tests/queryset/queryset.py
--- a/tests/queryset/queryset.py
+++ b/tests/queryset/queryset.py
@@ -4602,9 +4602,6 @@ class Doc(Document):
         self.assertEqual(doc_objects, Doc.objects.from_json(json_data))
 
     def test_json_complex(self):
-        if pymongo.version_tuple[0] <= 2 and pymongo.version_tuple[1] <= 3:
-            raise SkipTest("Need pymongo 2.4 as has a fix for DBRefs")
-
         class EmbeddedDoc(EmbeddedDocument):
             pass
 
diff --git a/tests/test_connection.py b/tests/test_connection.py
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -285,14 +285,7 @@ def test_connection_pool_via_kwarg(self):
         """Ensure we can specify a max connection pool size using
         a connection kwarg.
         """
-        # Use "max_pool_size" or "maxpoolsize" depending on PyMongo version
-        # (former was changed to the latter as described in
-        # https://jira.mongodb.org/browse/PYTHON-854).
-        # TODO remove once PyMongo < 3.0 support is dropped
-        if pymongo.version_tuple[0] >= 3:
-            pool_size_kwargs = {'maxpoolsize': 100}
-        else:
-            pool_size_kwargs = {'max_pool_size': 100}
+        pool_size_kwargs = {'maxpoolsize': 100}
 
         conn = connect('mongoenginetest', alias='max_pool_size_via_kwarg', **pool_size_kwargs)
         self.assertEqual(conn.max_pool_size, 100)
@@ -301,9 +294,6 @@ def test_connection_pool_via_uri(self):
         """Ensure we can specify a max connection pool size using
         an option in a connection URI.
         """
-        if pymongo.version_tuple[0] == 2 and pymongo.version_tuple[1] < 9:
-            raise SkipTest('maxpoolsize as a URI option is only supported in PyMongo v2.9+')
-
         conn = connect(host='mongodb://localhost/test?maxpoolsize=100', alias='max_pool_size_via_uri')
         self.assertEqual(conn.max_pool_size, 100)