我是pymongo和mongodb的新手。我试图用pymongo交互地插入文档。当我通过python脚本尝试文档时,它失败了。在
输入名称:
测试3
输入地址:Someaddress
输入电话号码:9876543{"Name": "test3", "Phone": 9876543, "Address": "Someaddress"} # Here I am printing the document to be inserted
Traceback (most recent call last):
File "./sample.py", line 54, in
insertDocument()
File "./sample.py", line 27, in insertDocument
insert_id=db.collection.insert_one(obj)
File "/appl/swinstall/mongo-python-driver-master/pymongo/collection.py", line 676, in insert_one
common.validate_is_document_type("document", document)
File "/appl/swinstall/mongo-python-driver-master/pymongo/common.py", line 434, 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
以下是我的代码:
^{pr2}$
但是当我在python终端中尝试同样的方法时,它被插入了。在test={"Name":"Test3", "Phone":98765653, "Address":"SomeAddress"}
insert_id=db.collection.insert_one(test)
x=db.collection.find()
x.count()
5
for y in x:
... print y
...
{u'Phone': 123456, u'_id': ObjectId('5ac59b23788b851887bb4a77'), u'Name': u'test', u'Address': u'DHL'}
{u'Phone': 5678990, u'_id': ObjectId('5ac5be03788b851c9f7da382'), u'Name': u'test2', u'Address': u'Cyberjaya'}
{u'Phone': 7872476, u'_id': ObjectId('5ac5be3b788b851c9f7da383'), u'Name': u'test3', u'Address': u'Kuala Lumpur'}
{u'Phone': 8977101750L, u'_id': ObjectId('5ac5dc59788b8523141b43eb'), u'Name': u'Soumya', u'Address': u'Cybersquare'}
{u'Phone': 98765653, u'_id': ObjectId('5acace4a788b855f05c7ea0d'), u'Name': u'Test3', u'Address': u'SomeAddress'} <== This got inserted
有人能帮忙吗?在