Skip to main content

Command Palette

Search for a command to run...

MongoDB

Published
3 min readView as Markdown

To use MongoDB, we need to install a library named pymongoand dnspython on Pycharm.

Pymongo basic code

from pymongo import MongoClient
client = MongoClient('여기에 URL 입력')
db = client.dbsparta

  • create a new Python file named 'dbprac' and paste the Pymongo basic code there.

image.png

  • The URL there should be the one below.

image.png

from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:<password>@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta
  • let's add data to the code and run the code.
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

doc = {
    'name':'bob',
    'age':27
}

db.users.insert_one(doc)
  • To check if the data added successfully, click on 'Browse Collections'

image.png

image.png


Inputting data to MongoDB.

  • Instead of writing data like the below,
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

doc = {'name':'bob', 'age':27}
db.users.insert_one(doc)

we can directly write a code in the brackets. (but not a commonly used way)

from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

db.users.insert_one({'name':'bobny', 'age':27})

Look for data

user = db.users.find_one({'name':'bobby'})

  • let's add more data and run it.
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

db.users.insert_one({'name':'bobby', 'age':27})
db.users.insert_one({'name':'john', 'age':20})
db.users.insert_one({'name':'ann', 'age':20})

image.png


Look for multiple data

all_users = list(db.users.find({}, {'_id':False}))

  • to see all the users
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

all_users = list(db.users.find({}))

for user in all_users:
    print(user)

image.png

  • to see the result without {'_id' : ~}
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

all_users = list(db.users.find({}, {'_id':False}))

for user in all_users:
    print(user)

image.png


what if I want to see only one data about bobby instead of all?

from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

user = db.users.find_one({'name':'bobby'})
print(user)

image.png

from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

user = db.users.find_one({'name':'bobby'})
print(user['age'])

image.png


Update; Pymongo(update_one)

code

# 오타가 많으니 이 줄을 복사해서 씁시다!
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

user = db.users.find_one({'name':'bobby'})
print(user)
  • let's run a code, db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

db.users.update_one({'name':'bobby'},{'$set':{'age':19}}) mean update data that has a name as bobby in users by changing age to 19

image.png


Delete; Pymongo(delete_one)

code

db.users.delete_one({'name':'bobby'})
from pymongo import MongoClient
client = MongoClient('mongodb+srv://test:sparta@cluster0.grgzlle.mongodb.net/?retryWrites=true&w=majority')
db = client.dbsparta

db.users.delete_one({'name':'bobby'})

Summary of Pymongo code

# 저장 - 예시
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

# 한 개 찾기 - 예시
user = db.users.find_one({'name':'bobby'})

# 여러개 찾기 - 예시 ( _id 값은 제외하고 출력)
all_users = list(db.users.find({},{'_id':False}))

# 바꾸기 - 예시
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

# 지우기 - 예시
db.users.delete_one({'name':'bobby'})

More from this blog

Ollie Seongyong Kim

85 posts