1
client = MongoClient(conn)
db = client.get_default_database()

json_file = {'test_1':1, 'test_2':2}
db.insert_one(json_file)

生成:

TypeError: 'Collection' object is not callable. If you meant to call the 'insert_many' method on a 'Database' object it is failing because no such method exists.

检查我的 pymongo 版本:

$ pip freeze | grep pymongo
$ pymongo==3.2.2

我认为这意味着insert_oneandinsert_many方法应该可用(post pymongo 3.0,对吗?)。更令人困惑的是,当我打电话时,dir(db)我根本看不到对任何insert方法的任何引用。

我错过了什么?

4

1 回答 1

2

那是因为db引用您的数据库,您需要使用点符号访问集合对象:

db.col.insert_one(json_file)

或类似字典的样式:

db["col"].insert_one(json_file)

col您的收藏名称在哪里。

于 2016-04-20T21:18:18.043 回答