python how MongoDB connection

MongoDB MongoDB need to be connected Python drive, here we use PyMongo drive to connect.

pip install:

pip3 install pymongo

Import library:

 

import pymongo

Connection to MongoDB:

 

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

Creating a database:

mydb = myclient["mydb"]

Note:  In MongoDB, a database is created only after content insertion is to say, after the database is created to create a collection (data sheet) and insert a document (record), the database will really create!.

Create a collection:

mycol = mydb["test"]

MongoDB use database objects to create a collection:

Note:  ! In MongoDB collection is created only when content is inserted that is, create a collection (data sheet) again after the insertion of a document (record), the collection will really create.

Into the document:

mydict = { "name": "RUNOOB", "alexa": "10000", "url": "https://www.runoob.com" }

x = mycol.insert_one(mydict)

So far, it has been connected to the database and create a collection, the database CRUD operations.

 

Guess you like

Origin www.cnblogs.com/nmsghgnv/p/11287925.html