In [1]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
MongoDB Docker Tutorial¶
Objective¶
- Docker를 활용한 MongoDB 실행
- MongoDB Basics & Shell Command
- PyMongo를 활용한 Python MongoDB Client 사용법
1. Docker를 활용한 MongoDB 실행¶
# MongoDB image 다운로드
$ docker pull mongo
# MongoDB image 확인
$ docker images
# MongoDB Container 구동
$ docker run --name mongodb-container -v ~/data_db:/data/db -d -p 27017:27017 mongo
# MongoDB Docker 컨테이너 중지
$ docker stop mongodb-container
# MongoDB Docker 컨테이너 시작
$ docker start mongodb-container
# MongoDB Docker 컨테이너 재시작
$ docker restart mongodb-container
# MongoDB Docker 컨테이너 접속
$ docker exec -it mongodb-container bash
2. MongDB Basics & Shell Commands¶
2-1. MongoDB Basics¶
- MongoDB 구조 : Database > Collections > Documents
- Database
- MongoDB에서 Database는 documents들의 collections으로 구성되어 있으며, 여러개의 databases 생성할 수 있습니다.
- Collections
- Collections 는 RDB의 tables과 같으며 documents 형태로 데이터를 저장합니다. 하나의 database는 여러개의 collections을 저장할 수 있습니다.
- Documetns
- MongoDB 에서 데이터 레코들들이 BSON documents의 형태로 저장됩니다. BSON 은 binary representation of JSON documents의 약자이며 JSON에 비해 더 많은 데이터 타입을 포함합니다. document는 field-value 혹은 key-value 형태로 만들어지며 이때 value은 아무 BSON 타입이 될 수 있습니다.
- Database
- Schemaless:
- MongoDB databases 스키마가 없습니다. 그러므로 collection내에서 하나의 document의 스키마와 다른 document의 스키마가 일치할 필요가 없습니다. 하나의 collection 내에서 다양한 형태의 documents를 포함하니다.
- https://www.geeksforgeeks.org/mongodb-database-collection-and-document/
2-2. MongoDB Shell Commands¶
# Docker Container 에서 mongo db 접속
mongo
# mongodb database 확인
show dbs
# test database 생성 및 전환 (실제 생성 시점은 database에 content가 추가될때)
use test
# test database에서 'col' collection 생성 및 document 저장
db.col.insertOne({"hello" : ['hi','sad','happy'], '_id' : '0'})
db.col.insertOne({"hello" : ['hi','sad','happy','for real?'], '_id' : '1'})
# document 확인
db.col.find();
# id 로 찾기
db.col.find({'_id' : "0"})
db.col.find({'_id' : "1"})
3. PyMongo를 활용한 Python MongoDB Client 사용법¶
3-1. PyMongo 설치¶
$ pip install pymongo
3-2. Python MongoDB Client 실행¶
from pymongo import MongoClient
# 방법1 - URI
mongodb_URI = "mongodb://localhost:27017/"
client = MongoClient(mongodb_URI)
# 방법2 - HOST, PORT
client = MongoClient(host='localhost', port=27017)
3-2. Python MongoDB Client api¶
In [1]:
from pymongo import MongoClient
client = MongoClient(host='localhost', port=27017)
In [2]:
# MongoDB Database 확인
for db in client.list_databases():
print(db)
{'name': 'admin', 'sizeOnDisk': 40960, 'empty': False}
{'name': 'config', 'sizeOnDisk': 110592, 'empty': False}
{'name': 'local', 'sizeOnDisk': 73728, 'empty': False}
{'name': 'test', 'sizeOnDisk': 8192, 'empty': False}
Database & Collection 생성 및 삭제¶
In [3]:
# MongoDB Database 생성
# 실제 database 생성 시점은 data가 database에 존재해야함
db = client["database2"]
# collection 생성
col = db["collection1"]
# document 생성
doc = {"hello" : "world", "hi" : "world"}
# document mongodb
x = col.insert_one(doc)
In [4]:
# "database2" DB 생성 확인
for db in client.list_databases():
print(db)
{'name': 'admin', 'sizeOnDisk': 40960, 'empty': False}
{'name': 'config', 'sizeOnDisk': 110592, 'empty': False}
{'name': 'database2', 'sizeOnDisk': 8192, 'empty': False}
{'name': 'local', 'sizeOnDisk': 73728, 'empty': False}
{'name': 'test', 'sizeOnDisk': 8192, 'empty': False}
In [5]:
# MongoDB 'database2' Database 삭제
print("List of databases before deletion\n--------------------------")
for x in client.list_database_names():
print(x)
#delete database named 'test'
client.drop_database('database2')
print("\nList of databases after deletion\n--------------------------")
for x in client.list_database_names():
print(x)
List of databases before deletion
--------------------------
admin
config
database2
local
test
List of databases after deletion
--------------------------
admin
config
local
test
In [6]:
# MongoDB 'test' Database Collection 확인
mydb = client['test']
print("List of collections\n--------------------")
#list the collections
for coll in mydb.list_collection_names():
print(coll)
List of collections
--------------------
col
In [7]:
# MongoDB 'test' Database 'col2' Collection 추가
mydb = client['test']
col2 = mydb['col2']
doc2 = {"hello" : ['hi','sad','happy','for real?'], '_id' : '0', 'hihi' : 'real'}
col2.insert_one(doc2)
Out[7]:
<pymongo.results.InsertOneResult at 0x7fec73589af0>
In [8]:
print("List of collections\n--------------------")
for coll in mydb.list_collection_names():
print(coll)
List of collections
--------------------
col2
col
In [9]:
# MongoDB 'test' Database 'col2' Collection 삭제
print("List of collections before deletion\n--------------------------")
for x in mydb.list_collection_names():
print(x)
#get collection named "developers"
mycol = mydb["col2"]
#delete or drop collection
mycol.drop()
print("\nList of collections after deletion\n--------------------------")
for x in mydb.list_collection_names():
print(x)
List of collections before deletion
--------------------------
col2
col
List of collections after deletion
--------------------------
col
Document 생성 (Single/Multi)¶
In [10]:
# "test" Database - "col" collection - update single document
test_col = client['test']['col']
document = { "name": "Kiku", "address": "Germany" }
test_col.insert_one(document)
Out[10]:
<pymongo.results.InsertOneResult at 0x7fec735aabe0>
In [11]:
# print all the documents in the collection
for x in test_col.find():
print(x)
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
In [12]:
# find document by _id
result = test_col.find({'_id' : '1'})
for doc in result:
print(doc)
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
In [13]:
# Update multiple Document
#list of documents
arr = [{ "name": "Kiku", "address": "Germany" },
{ "name": "Lini", "address": "Sweden" },
{ "name": "Sree", "address": "India" },
{ "name": "Raghu", "address": "India" },
{ "name": "Mams", "address": "France" }]
#insert multiple documents
x = test_col.insert_many(arr)
print('ids of inserted documents\n---------------------')
for id in x.inserted_ids:
print(id)
ids of inserted documents
---------------------
62b3de87676c0c68074434d9
62b3de87676c0c68074434da
62b3de87676c0c68074434db
62b3de87676c0c68074434dc
62b3de87676c0c68074434dd
In [14]:
# print all the documents in the collection
for x in test_col.find():
print(x)
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434d9'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434da'), 'name': 'Lini', 'address': 'Sweden'}
{'_id': ObjectId('62b3de87676c0c68074434db'), 'name': 'Sree', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dc'), 'name': 'Raghu', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dd'), 'name': 'Mams', 'address': 'France'}
Document 삭제 (Single/Multi)¶
In [16]:
from bson.objectid import ObjectId
In [17]:
# "test" Database - "col" collection - delete single document
test_col = client['test']['col']
print('Documents in Collection\n-----------------------')
for doc in test_col.find():
print(doc)
# delete '_id': ObjectId('62b3de87676c0c68074434dd')
query = {'_id' : ObjectId('62b3de87676c0c68074434dd')}
test_col.delete_one(query)
print('\nDocuments in Collection after delete_one()\n-----------------------')
for doc in test_col.find():
print(doc)
Documents in Collection
-----------------------
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434d9'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434da'), 'name': 'Lini', 'address': 'Sweden'}
{'_id': ObjectId('62b3de87676c0c68074434db'), 'name': 'Sree', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dc'), 'name': 'Raghu', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dd'), 'name': 'Mams', 'address': 'France'}
Documents in Collection after delete_one()
-----------------------
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434d9'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434da'), 'name': 'Lini', 'address': 'Sweden'}
{'_id': ObjectId('62b3de87676c0c68074434db'), 'name': 'Sree', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dc'), 'name': 'Raghu', 'address': 'India'}
In [18]:
# "test" Database - "col" collection - delete multiple document
print('Documents in Collection\n-----------------------')
for doc in test_col.find():
print(doc)
# query to delete document
query = {'address':'India'}
#delete many document
test_col.delete_many(query)
print('\nDocuments in Collection after delete_many ()\n-----------------------')
for doc in test_col.find():
print(doc)
Documents in Collection
-----------------------
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434d9'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434da'), 'name': 'Lini', 'address': 'Sweden'}
{'_id': ObjectId('62b3de87676c0c68074434db'), 'name': 'Sree', 'address': 'India'}
{'_id': ObjectId('62b3de87676c0c68074434dc'), 'name': 'Raghu', 'address': 'India'}
Documents in Collection after delete_many ()
-----------------------
{'_id': '0', 'hello': ['hi', 'sad', 'happy']}
{'_id': '1', 'hello': ['hi', 'sad', 'happy', 'for real?']}
{'_id': ObjectId('62b3de7f676c0c68074434d8'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434d9'), 'name': 'Kiku', 'address': 'Germany'}
{'_id': ObjectId('62b3de87676c0c68074434da'), 'name': 'Lini', 'address': 'Sweden'}
댓글