PYTHON-1853 Empty projections should return the entire document not just the _id (#738)
This commit is contained in:
parent
fcedc510e1
commit
c7d80802be
@ -143,6 +143,13 @@ Breaking Changes in 4.0
|
||||
opposed to
|
||||
the previous syntax which was simply ``if collection:`` or ``if database:``.
|
||||
You must now explicitly compare with None.
|
||||
- Empty projections (eg {} or []) for
|
||||
:meth:`~pymongo.collection.Collection.find`, and
|
||||
:meth:`~pymongo.collection.Collection.find_one`
|
||||
are passed to the server as-is rather than the previous behavior which
|
||||
substituted in a projection of ``{"_id": 1}``. This means that an empty
|
||||
projection will now return the entire document, not just the ``"_id"`` field.
|
||||
|
||||
|
||||
Notable improvements
|
||||
....................
|
||||
|
||||
@ -637,6 +637,22 @@ Can be changed to this::
|
||||
|
||||
You must now explicitly compare with None.
|
||||
|
||||
Collection.find returns entire document with empty projection
|
||||
.............................................................
|
||||
Empty projections (eg {} or []) for
|
||||
:meth:`~pymongo.collection.Collection.find`, and
|
||||
:meth:`~pymongo.collection.Collection.find_one`
|
||||
are passed to the server as-is rather than the previous behavior which
|
||||
substituted in a projection of ``{"_id": 1}``. This means that an empty
|
||||
projection will now return the entire document, not just the ``"_id"`` field.
|
||||
To ensure that behavior remains consistent, code like this::
|
||||
|
||||
coll.find({}, projection={})
|
||||
|
||||
Can be changed to this::
|
||||
|
||||
coll.find({}, projection={"_id":1})
|
||||
|
||||
SONManipulator is removed
|
||||
-------------------------
|
||||
|
||||
|
||||
@ -1246,6 +1246,10 @@ class Collection(common.BaseObject):
|
||||
|
||||
.. versionchanged:: 4.0
|
||||
Removed the ``modifiers`` option.
|
||||
Empty projections (eg {} or []) are passed to the server as-is,
|
||||
rather than the previous behavior which substituted in a
|
||||
projection of ``{"_id": 1}``. This means that an empty projection
|
||||
will now return the entire document, not just the ``"_id"`` field.
|
||||
|
||||
.. versionchanged:: 3.11
|
||||
Added the ``allow_disk_use`` option.
|
||||
|
||||
@ -195,8 +195,6 @@ class Cursor(object):
|
||||
allow_disk_use = validate_boolean("allow_disk_use", allow_disk_use)
|
||||
|
||||
if projection is not None:
|
||||
if not projection:
|
||||
projection = {"_id": 1}
|
||||
projection = helpers._fields_list_to_dict(projection, "projection")
|
||||
|
||||
self.__spec = spec
|
||||
|
||||
@ -1707,7 +1707,10 @@ class TestCollection(IntegrationTest):
|
||||
self.assertTrue("hello" in db.test.find_one(projection=frozenset(["hello"])))
|
||||
self.assertTrue("hello" not in db.test.find_one(projection=frozenset(["foo"])))
|
||||
|
||||
self.assertEqual(["_id"], list(db.test.find_one(projection=[])))
|
||||
self.assertEqual(["_id"], list(db.test.find_one(projection={'_id':
|
||||
True})))
|
||||
self.assertTrue("hello" in list(db.test.find_one(projection={})))
|
||||
self.assertTrue("hello" in list(db.test.find_one(projection=[])))
|
||||
|
||||
self.assertEqual(None, db.test.find_one({"hello": "foo"}))
|
||||
self.assertEqual(None, db.test.find_one(ObjectId()))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user