Pymongo importing succeeded but not showing in the

2019-09-08 15:36发布

I tried importing json file which succeeded

mongoimport --db dbwhy --collection dbcol --jsonArray consumer_complaint.json
2016-01-15T19:00:42.277-0600    connected to: localhost
2016-01-15T19:00:42.320-0600    imported 34 documents

but when I tried viewing it, it was not there

from pymongo import MongoClient

client = MongoClient('localhost',27017)
db = client['dbwhy']
coll = db['dbcol']
curs = db.coll.find()
for i in curs:
    print(i)

It does not show anything

1条回答
在下西门庆
2楼-- · 2019-09-08 16:02

The problem is here:

db.coll.find()

This would find all documents inside the coll collection, but your collection is named dbcol.

Instead, use the coll variable that you've already defined:

from pymongo import MongoClient

client = MongoClient('localhost',27017)

db = client['dbwhy']
coll = db['dbcol']

curs = coll.find()  # FIX is here
for i in curs:
    print(i)
查看更多
登录 后发表回答