We give a somewhat helpful error when the `documents` argument is not an iterable type (or an empty iterable):
>>> client.t.t.insert_many(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 746, in insert_many raise TypeError("documents must be a non-empty list") TypeError: documents must be a non-empty list >>> client.t.t.insert_many([]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 746, in insert_many raise TypeError("documents must be a non-empty list") TypeError: documents must be a non-empty list >>> client.t.t.insert_many({}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 746, in insert_many raise TypeError("documents must be a non-empty list") TypeError: documents must be a non-empty list
However when passing a single document (or RawBSONDocument) to `insert_many` we get this unhelpful error:
>>> client.t.t.insert_many({'_id':2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 760, in insert_many
blk.ops = [doc for doc in gen()]
File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 760, in <listcomp>
blk.ops = [doc for doc in gen()]
File "/Users/shane/git/mongo-python-driver/pymongo/collection.py", line 751, in gen
common.validate_is_document_type("document", document)
File "/Users/shane/git/mongo-python-driver/pymongo/common.py", line 502, in validate_is_document_type
raise TypeError("%s must be an instance of dict, bson.son.SON, "
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
>>> client.test.test.insert_many(RawBSONDocument(bson.BSON.encode({'_id':2})))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pymongo/collection.py", line 753, in insert_many
blk.ops = [doc for doc in gen()]
File "pymongo/collection.py", line 744, in gen
common.validate_is_document_type("document", document)
File "pymongo/common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
This issue caused some confusion in https://github.com/mongodb-labs/python-bsonjs/issues/14
We should make the error message more helpful by better indicating what the problem is, maybe something like this:
>>> client.t.t.insert_many({'_id': 2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
TypeError: documents must be a list, not a single document
- is duplicated by
-
PYTHON-2407 Improve error message when attempting to insert document using insert_many
-
- Closed
-