【问题标题】:Error while inserting data in mongodb with pymongo使用 pymongo 在 mongodb 中插入数据时出错
【发布时间】:2018-09-09 09:57:36
【问题描述】:

我是 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 <module>
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

下面是我的代码:

#!/usr/bin/python

import sys
import os
from pymongo import MongoClient
ch=MongoClient()
db=ch.local

def insertDocument():
print "insertDocument"
print "Enter the name:"
name=raw_input(">> ")
print "Enter the address"
address=raw_input(">> ")
print "Enter the phone number"
phone=raw_input(">> ")
obj='{"Name": "'+ str(name) +'", "Phone": '+ str(phone) +', "Address": "'+ str(address) +'"}'
print obj
insert_id=db.collection.insert_one(obj)

insertDocument()

但是当我在 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

有人可以帮忙吗?

【问题讨论】:

  • db.collection.insert_one('obj') 您正在传递要插入的字符串 obj。插入参数必须是 dict。删除 obj 周围的引号。
  • 感谢您的回复。删除了单引号。还是错误。 {"Name": "test4", "Phone": 9864362, "Address": "someaddr"} Traceback(最近一次调用最后):文件“./sample.py”,第 54 行,在 insertDocument()文件“./sample.py”,第 27 行,insertDocument insert_id=db.collection.insert_one(obj) 文件“/appl/swinstall/mongo-python-driver-master/pymongo/collection.py”,第 676 行,insert_one common.validate_is_document_type("document ”,文档)文件“/appl/swinstall/mongo-python-driver-master/pymongo/common.py”,第 434 行,在 validate_is_document_type...
  • obj 值是字符串,应该是dict!
  • obj='...' # 这就是问题所在

标签: python mongodb pymongo


【解决方案1】:

通过以下方式更改您的代码:

insert_id=db.collection.insert_one(json.loads(obj))

【讨论】:

    【解决方案2】:

    错误信息给出了提示。它说 obj 必须是一个字典。在交互式方法中,您确实在定义一个字典,但在脚本中您正在创建一个字符串。该字符串看起来像一个字典,但实际上不是。

    您可以通过打印来确认:

    打印(类型(obj))

    因此,解决方案是将其存储在带有键值对的字典中。比如:

    obj = {}

    obj["name"]=str(name)

    等等……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2016-04-23
      • 2021-02-01
      相关资源
      最近更新 更多