- pymongo.readthedocs.io/en/stable/atlas.html
- 아틀라스 로그인
mongodb+srv://jinhae:<password>@jinhae-cluster.lsfcp.mongodb.net/<dbname>?retryWrites=true&w=majority
- 비밀번호랑 디비이름 수정
mongodb+srv://jinhae:qscft7259*@jinhae-cluster.lsfcp.mongodb.net/jinhae-db?retryWrites=true&w=majority
import pymongo
# 아래 atlas URI 서식을 위해 dnspython 라이브러리를 설치해야 함
client = pymongo.MongoClient('mongodb+srv://jinhae:qscft7259*@jinhae-cluster.lsfcp.mongodb.net/jinhae-db?retryWrites=true&w=majority')
for name in client.list_database_names():
print(name)
www.c-sharpcorner.com/article/mongodb-atlas-with-python/
from pymongo import MongoClient
from datetime import datetime
client = MongoClient('mongodb+srv://jinhae:qscft7259*@jinhae-cluster.lsfcp.mongodb.net/jinhae-db?retryWrites=true&w=majority')
collection = client.test.books
booksData = [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
'published_date': datetime.now()
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy",
'published_date': datetime.now()
}
]
collection.insert_many(booksData)
collection = client.test.books
※ test, books 자동으로 아틀라스에 생김
test: database name / books: collection name
- 오류남
collection = client.jinhae-db.book
-해결
db = client['jinhae-db'] # db name
collection = db['book'] #collection name
from pymongo import MongoClient
from datetime import datetime
client = MongoClient('mongodb+srv://jinhae:qscft7259*@jinhae-cluster.lsfcp.mongodb.net/'
'jinhae-db?retryWrites=true&w=majority')
year = datetime.year
month = datetime.month
day = datetime.day
hour = datetime.hour
minute = datetime.minute
second = datetime.second
datetime = f'{year}-{month}-{day} {hour}:{minute}:{second}'
collection = client.test.books
booksData = [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt",
'published_date': datetime
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy",
'published_date': datetime
}
]
collection.insert_many(booksData)
- published_date가 달라서 추가로 업데이트 됨
'mongodb' 카테고리의 다른 글
몽고디비 3일차- django, 장고 (0) | 2021.01.18 |
---|---|
몽고디비 3일차- 웹연동, postman (0) | 2021.01.18 |
몽고디비 3일차- studio 3t, mongodb tool (0) | 2021.01.18 |
몽고디비 2일차 - 크롤링 (0) | 2021.01.15 |
몽고디비 1일차 (0) | 2021.01.14 |