使用 pymongo 将 JSON 导出到 Mongo 时遇到问题
Trouble exporting JSON to Mongo with pymongo
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_one
和 insert_many
方法应该可用(post pymongo 3.0
,对吧?)。更令人困惑的是,当我调用 dir(db)
时,我根本看不到任何对任何 insert
方法的引用。
我错过了什么?
那是因为db
指的是你的数据库,你需要用点号访问collection object:
db.col.insert_one(json_file)
或 dictionary-like 风格:
db["col"].insert_one(json_file)
其中 col
是您 collection 的名称。
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_one
和 insert_many
方法应该可用(post pymongo 3.0
,对吧?)。更令人困惑的是,当我调用 dir(db)
时,我根本看不到任何对任何 insert
方法的引用。
我错过了什么?
那是因为db
指的是你的数据库,你需要用点号访问collection object:
db.col.insert_one(json_file)
或 dictionary-like 风格:
db["col"].insert_one(json_file)
其中 col
是您 collection 的名称。