我试图将数据从SQL Server迁移到MongoDB,但在将数据导入MongoDB时,在最后阶段遇到了类型错误。
mongoImp = dbo.insert_many(jArray)
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/collection.py", line 710, in insert_many
blk.ops = [doc for doc in gen()]
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/collection.py", line 702, in gen
common.validate_is_document_type("document", document)
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/common.py", line 407, 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
我还检查了type(jArray)
str
。尝试将数据类型转换为list
,但无法成功。
我的代码:
import pyodbc
import json
import collections
import pymongo
from bson import json_util
odbcArray = []
mongoConStr = '192.168.10.107:36006'
sqlConStr = 'DRIVER={MSSQL-NC1311};SERVER=tcp:192.168.10.103,57967;DATABASE=AdventureWorks;UID=testuser;PWD=testuser'
mongoConnect = pymongo.MongoClient(mongoConStr)
sqlConnect = pyodbc.connect(sqlConStr)
dbo = mongoConnect.eaedw.sqlData
dbDocs = dbo.find()
sqlCur = sqlConnect.cursor()
sqlCur.execute("""
SELECT TOP 2 BusinessEntityID,Title, Demographics, rowguid, ModifiedDate
FROM Person.Person
""")
tuples = sqlCur.fetchall()
for tuple in tuples:
doc = collections.OrderedDict()
doc['id'] = tuple.BusinessEntityID
doc['title'] = tuple.Title
doc['dgrap'] = tuple.Demographics
doc['rowi'] = tuple.rowguid
doc['mtime'] = tuple.ModifiedDate
odbcArray.append(doc)
jArray = json.dumps(odbcArray, default=json_util.default)
mongoImp = dbo.insert_many(jArray)
mongoConnect.close()
sqlConnect.close()
答案 0 :(得分:3)
从MongoDB的网页上查看此bulk insert example。跳过json.dumps
调用(将您的文档数组转换为json格式的字符串)并直接插入odbcArray
:
mongoImp = dbo.insert_many(odbcArray)