aaron
aaron

Reputation: 6489

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)

generates:

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.

Checking my version of pymongo:

$ pip freeze | grep pymongo
$ pymongo==3.2.2

Which I think means that the insert_one and insert_many methods should be available (post pymongo 3.0, right?). More confusingly, when I call dir(db) I don't see any references to any insert methods at all.

What am I missing?

Upvotes: 1

Views: 684

Answers (1)

alecxe
alecxe

Reputation: 474191

That's because db refers to your database, you need to access a collection object with a dot notation:

db.col.insert_one(json_file)

or in a dictionary-like style:

db["col"].insert_one(json_file)

where col is the name of your collection.

Upvotes: 2

Related Questions