The version of Python on my Mac is 2.7.10, so I need to match the version and install pymongo by the following method.
sudo pip install pymongo==2.7.2
Connect to the local MongoDB, and if there is a DB called hoge on MongoDB, write as follows to get the connection.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pymongo import Connection
#Get mongodb connection
con = Connection('localhost', 27017)
db = con.hoge
If there is a collection called hoge_mst in a DB called hoge, get the collection as follows.
col = db.hoge_mst
print col.find_one()
for item in col.find():
print item
key1
If you want to get the value only for the column in the collection
for item in col.find():
print item['key1']
itemid
Get only those with a key of 2
for item in col.find({'itemID':2}):
print item['key1']
Recommended Posts