Python으로 배우는 MongoDB 입문
Filip Schouwenaars
Machine Learning Researcher
filmmovies
from pymongo import MongoClient client = MongoClient()# 사용 가능한 데이터베이스 나열 client.list_database_names()
['admin', 'config', 'film', 'local']
# film DB의 컬렉션 나열
client.film.list_collection_names()
['movies']
# 클라이언트 설정 from pymongo import MongoClient client = MongoClient()# 모든 문서 반환 client.film.movies.find()# 점 표기 줄이기 mov = client.film.moviesmov.find()
# 클라이언트 설정
from pymongo import MongoClient
client = MongoClient()
mov = client.film.movies
# 모든 문서 조회
mov.find()
<pymongo.synchronous.cursor.Cursor at 0x7f...760>
list(mov.find())
[{'_id': '68...ff', 'title': 'superbad', ...
# 클라이언트 설정
from pymongo import MongoClient
client = MongoClient()
mov = client.film.movies
# find(), 커서를 리스트로 변환
list(mov.find())
[
{
"_id": "6824bb...e53adbf274ff",
"title": "superbad",
"genre": ["comedy", "teen"],
"release_year": 2007,
"rating": 7.6
},
{
"_id": "6824bb...e53adbf27500",
"title": "interstellar",
"genre": ["adventure", "drama", "sci-fi"],
"release_year": 2014,
"rating": 8.6,
"won_oscar": True
},
...
]
from pymongo import MongoClient client = MongoClient() mov = client.film.moviescurs = mov.find({ "won_oscar": True }) list(curs)
[
{
'_id': '6824bbd05644e53adbf27500',
'genre': ['adventure', 'drama', 'sci-fi'],
'rating': 8.6,
'release_year': 2014,
'title': 'interstellar',
'won_oscar': True
},
{
'_id': '6824bbd05644e53adbf27501',
'genre': ['action', 'sci-fi', 'thriller'],
'rating': 8.8,
'release_year': 2010,
'title': 'inception',
'won_oscar': True
},
...
]
from pymongo import MongoClient client = MongoClient() mov = client.film.moviesmov.find_one({ "title": "parasite" })
{
'_id': '6824bbd05644e53adbf27518',
'genre': ['drama', 'thriller'],
'rating': 8.5,
'release_year': 2019,
'title': 'parasite',
'won_oscar': True
}
Python으로 배우는 MongoDB 입문