Try working with Mongo in Python on Mac

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

Get connection

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

Get collection

If there is a collection called hoge_mst in a DB called hoge, get the collection as follows.

col = db.hoge_mst

Getting data from a collection

Get only one

print col.find_one()

Get all

for item in col.find():
	print item

I want to get all items and use only the necessary items

key1If you want to get the value only for the column in the collection

for item in col.find():
	print item['key1']

I want to specify the search conditions and get

itemidGet only those with a key of 2

for item in col.find({'itemID':2}):
	print item['key1']

Recommended Posts

Try working with Mongo in Python on Mac
Try working with binary data in Python
Working with sounds in Python
Install selenium on Mac and try it with python
Working with LibreOffice in Python: import
Working with DICOM images in Python
python on mac
Try logging in to qiita with Python
Follow active applications on Mac with Python
Post Test 3 (Working with PosgreSQL in Python)
Build Python environment with Anaconda on Mac
Working with GPS on Raspberry Pi 3 Python
Install Python on Mac
Install Python 3 on Mac
Try gRPC in Python
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Try 9 slices in Python
Install Python 3.4 on Mac
Try importing MLB data on Mac and Python
Try deepdream on Mac
[Introduction for beginners] Working with MySQL in Python
Specific sample code for working with SQLite3 in Python
Try debugging Python on Raspberry Pi with Visual Studio.
Try implementing associative memory with Hopfield network in Python
Try embedding Python in a C ++ program with pybind11
I want to AWS Lambda with Python on Mac!
Scraping with selenium in Python
Scraping with chromedriver in python
Install pygame on python3.4 on mac
Debugging with pdb in Python
Try Python output with Haxe 3.2
Python3 + Django ~ Mac ~ with Apache
Python: Working with Firefox with selenium
[Python] Insert ":" in MAC address
Scraping with Selenium in Python
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Scraping with Tor in Python
Tweet with image in Python
Notes on installing Python on Mac
Combined with permutations in Python
Try running Python with Try Jupyter
Try implementing Yubaba in Python 3
Try face recognition with Python
Using OpenCV with Python @Mac
Try running python in a Django environment created with pipenv
Try scraping the data of COVID-19 in Tokyo with Python
Create a list in Python with all followers on twitter
Try sorting your own objects with priority queue in Python
Machine learning environment settings based on Python 3 on Mac (coexistence with Python 2)
VScode environment construction (on Mac) & graph display in Python (@browser)
Number recognition in images with Python
Building a Python environment on Mac
Testing with random numbers in Python
tensor flow with anaconda on mac
GOTO in Python with Sublime Text 3
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
Try to operate Facebook with Python
Create a Python environment on Mac (2017/4)