Python MongoDB

Time:2022-4-4

Python MongoDB
Mongodb is one of the most popular NoSQL databases at present. The data type used is bson (similar to JSON).

Mongodb database installation and introduction can view our mongodb tutorial.

PyMongo
Python needs a mongodb driver to connect to mongodb. Here we use pymongo driver to connect.

PIP installation
Pip is a general Python package management tool, which provides the functions of finding, downloading, installing and uninstalling Python packages.

Install pymongo:

$ python3 -m pip3 install pymongo
You can also specify the installed version:

$ python3 -m pip3 install pymongo==3.5.1
Update pymongo command:

$ python3 -m pip3 install –upgrade pymongo
easy_ Install install
Old versions of Python can use easy_ Install to install, easy_ Install is also a python package management tool.

$ python -m easy_install pymongo
Update pymongo command:

$ python -m easy_install -U pymongo
Test pymongo
Next, we can create a test file demo_ test_ mongodb. Py, the code is as follows:

demo_ test_ mongodb. Py file code:

!/usr/bin/python3

import pymongo
Execute the above code file. If there is no error, the installation is successful.

Create database
Create a database
To create a database, you need to use the mongoclient object, and specify the URL address of the connection and the name of the database to be created.

In the following example, we create the database runoobdb:

example

!/usr/bin/python3

import pymongo

myclient = pymongo.MongoClient(“mongodb://localhost:27017/”)
mydb = myclient[“runoobdb”]
Note: in mongodb, the database will be created only after the content is inserted! That is, after the database is created, a set (data table) should be created and a document (record) inserted before the database can be really created.

Judge whether the database already exists
We can read all databases in mongodb and judge whether the specified database exists:

example

!/usr/bin/python3

import pymongo

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

dblist = myclient.list_database_names()

dblist = myclient.database_names()

if “runoobdb” in dblist:
Print (“database already exists!”)
Note: Database_ Names is obsolete in the latest version of python, python 3 The version after 7 + is changed to list_ database_ names()。

Create collection
Collections in mongodb are similar to SQL tables.

Create a collection
Mongodb uses database objects to create collections. Examples are as follows:

example

!/usr/bin/python3

import pymongo

myclient = pymongo.MongoClient(“mongodb://localhost:27017/”)
mydb = myclient[“runoobdb”]

mycol = mydb[“sites”]
Note: in mongodb, the collection will be created only after the content is inserted! That is, after creating a set (data table), you need to insert another document (record) before the set can be really created.

Judge whether the collection already exists
We can read all sets in mongodb database and judge whether the specified set exists:

example

!/usr/bin/python3

import pymongo

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

mydb = myclient[‘runoobdb’]

collist = mydb. list_collection_names()

collist = mydb.collection_names()

If “sites” in collist: # judge whether the sites collection exists
Print (“collection already exists!”)

Recommended Today

Modul of fastems

Each module of fastems is implemented from the abstract class Fastems.Mms.Client.Infrastructure.UiModuleBase; public class DataManagerModule : UiModuleBase { public override void Initialize() { AddResourceDictionary(“/Resources/DataManagerResources.xaml”, typeof(DataManagerModule)); RegisterViewWithRegion(“DialogRegion”, typeof(DialogView)); RegisterViewWithRegion(“BusyIndicatorRegion”, typeof(BusyIndicatorView)); } } And Fastems.Mms.Client.Infrastructure.UiModuleBase inherits from Fastems.Mms.Client.Infrastructure.ModuleBase public abstract class UiModuleBase : ModuleBase { [Import] public IRegionManager RegionManager { get; set; } [Import] public IMergedDictionaryRegistry MergedDictionaryRegistry { […]