- pymongo.readthedocs.io/en/stable/atlas.html

 

Using PyMongo with MongoDB Atlas — PyMongo 3.11.2 documentation

Using PyMongo with MongoDB Atlas Atlas is MongoDB, Inc.’s hosted MongoDB as a service offering. To connect to Atlas, pass the connection string provided by Atlas to MongoClient: client = pymongo.MongoClient( ) Connections to Atlas require TLS/SSL. For co

pymongo.readthedocs.io

- 아틀라스 로그인 

- www.mongodb.com/cloud/atlas 

 

Managed MongoDB Hosting | Database-as-a-Service

Host MongoDB in the cloud with MongoDB Atlas, the official database-as-a-service, on AWS, Azure and GCP. Try free!

www.mongodb.com

 

 

copy


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

 

 

file- setting들어가서 
dnspython 설치

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/

 

MongoDB Atlas with Python

In this article, you will learn about MongoDB atlas with Python.

www.c-sharpcorner.com

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

+ Recent posts