MongoDB insert_many无法正常工作

时间:2018-08-05 12:17:11

标签: python python-3.x mongodb

我有一个文本文件,数据为 t1:v1 t2:v2 t3:v3 t1:v1 等等

我必须阅读文件并将其推入mongodb

我正在这样做

import glob
import sys
import pymongo
from pymongo import MongoClient

def main():

    if len(sys.argv) != 2:
        print('Usage: ./prog_name path')
    else:
        client = MongoClient('localhost', 9999)
        db = client['dbtag']
        collection = db['tctags']
        path = sys.argv[1]
        for tcname in glob.glob(path+'*.txt'):
            with open(tcname) as tfile:
                taglist = tfile.read().splitlines()
                d = {}
                for line in taglist:
                    tagsplit = line.split(':')
                    d[tagsplit[0]] = tagsplit[1]

                print(d, "\n\nonefiledone\n\n")
                print(type(d))
            result = collection.insert_many(d)

if __name__ == "__main__":
    main()

但是它在第25行insert_many上引发了错误:

TypeError: document must be an instance of dict, bson.son.SON,
           bson.raw_bson.RawBSONDocument, or a type that inherits from 
           collections.MutableMapping

编辑:

控制台输出

{'t1': 'v1', 't2': 'v2', 't3': 'v3'}

onefiledone


<class 'dict'>
Traceback (most recent call last):
  File ".\tagPython.py", line 29, in <module>
    main()
  File ".\tagPython.py", line 25, in main
    result = collection.insert_many(d)
  File "C:\Users\cmehar\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py", line 752, in insert_many
    blk.ops = [doc for doc in gen()]
  File "C:\Users\cmehar\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py", line 752, in <listcomp>
    blk.ops = [doc for doc in gen()]
  File "C:\Users\cmehar\AppData\Local\Continuum\anaconda3\lib\site-packages\pymongo\collection.py", line 743, in gen
    common.validate_is_document_type("document", document)
  File "C:\Users\cmehar\AppData\Local\Continuum\anaconda3\lib\site-packages\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

2 个答案:

答案 0 :(得分:1)

这是因为insert_many()需要文档列表(词典),但是您仅提供了一个词典。 您可以只使用insert()代替insert_many(),这应该可以。
或者,您可以将这些词典拆分为单独的文档,然后使用insert_many()

答案 1 :(得分:1)

d是一个文档,因此应该使用insert_one

使用insert_manyd的字段被迭代,并且每个字段都作为文档插入(由于它们不是字典而失败)。