Use MongoDB in Python applications and mongodb in python applications
Python is a powerful programming language used in many different types of applications in the development community. Many people know that it is a flexible language that can process almost any task. Therefore, what kind of database is needed in the Python application as flexible as the language itself? That is NoSQL, such as MongoDB.
Https://realpython.com/blog/python/introduction-to-mongodb-and-python.
1. SQL vs NoSQL
If you are not familiar with the concept of NoSQL, MongoDB is a NoSQL database. In recent years, it has become increasingly popular in the industry. NoSQL databases provide a very different retrieval method and data storage function from relational databases.
In the decades of emergence of NoSQL, sqldatabase is one of the only choices developers seek to build large and scalable systems. However, more and more requirements require the ability to store complex data structures. This promotes the emergence of NoSQL databases, allowing developers to store heterogeneous and unstructured data.
When selecting a database solution, most people ask their last question, "SQL or NoSQL ?". Both SQL and NoSQL have their own strengths and weaknesses. You should choose one of the best options that suit your application needs. Here are some differences between the two:
SQL
The model is relational;
Data is stored in tables;
It is applicable when each record is of the same type and has the same attributes;
The storage specification must have a predefined structure;
Adding new attributes means you must change the overall architecture;
ACID transaction support;
NoSQL
The model is non-relational;
Json and key-value equivalence can be stored (determined by the NoSQL database type );
Not every record must have the same structure;
Adding data with new attributes will not affect others;
ACID transactions are supported, which varies with the NoSQL databases used;
Consistency can be changed;
Horizontal scaling;
There are many other differences between the two types of databases, but the above mentioned are some more important differences. Depending on your situation, using SQL database may be the preferred choice, while in other cases, NoSQL is the more obvious choice. When selecting a database, you should carefully consider the advantages and disadvantages of each database.
One advantage of NoSQL is that there are many different types of databases to choose from, and each has its own use case:
Key-value storage: DynamoDB
Document storage: CouchDB, MongoDB, RethinkDB
Column store: Cassandra
Data Structure: Redis, SSDB
There are many more, but these are some of the more common types. In recent years, SQL and NoSQL databases have even begun to merge. For example, PostgreSQL now supports storing and querying JSON data, much like MongoDB. With this, you can use Postgres to implement the same functions as MongoDB, but you still do not have other advantages of MongoDB (such as horizontal resizing and simple interfaces ).
2. MongoDB
Now, let's focus on this article and clarify the specific situations of MongoDB.
MongoDB is a document-oriented, open-source database program that has nothing to do with the platform. MongoDB is like some other NoSQL databases (but not all !) Use JSON-formatted documents to store data. This makes the data very flexible and does not require a Schema.
Some important features are as follows:
Supports multiple standard query types, such as matching (), comparison (,), and regular expressions;
It can store almost any type of data, whether structured, partially structured, or even polymorphism;
To expand and process more queries, you only need to add more machines;
It is highly flexible and agile, allowing you to quickly develop applications;
As a document-based database, you can store all information about your model in a single document;
You can change the database Schema at any time;
Many relational database functions can also be used in MongoDB (such as indexes ).
In terms of operation, MongoDB does not have many functions in other databases:
Whether you need an independent server or a complete independent server cluster, MongoDB can be expanded as needed;
MongoDB also provides load balancing support by automatically moving data on each shard;
It supports automatic failover. If the master server is Down, the new master server will automatically start and run;
The MongoDB Management Service (MMS) can be used to monitor and back up MongoDB infrastructure services;
Unlike relational databases, because of memory ing files, you will save a lot of RAM.
Although MongoDB seems to be a database that solves many of our problems at first, it does not have any disadvantages. A common disadvantage of MongoDB is its lack of support for ACID transactions. MongoDB supports ACID transactions in specific scenarios, but not in all cases. At the single-document level, ACID transactions are supported (where most transactions occur ). However, due to the distributed nature of MongoDB, it does not support processing transactions of multiple documents.
MongoDB still lacks support for natural join queries. In MongoDB's view, documents are intended to be all-encompassing, which means that they do not need to be referenced in other documents. In the real world, this is not always effective because the data we use is relational. Therefore, many people think that MongoDB should be used as a complementary database for SQL databases, but when you use MongoDB, you will find that this is wrong.
3. PyMongo
Now we have described what MongoDB is. Let's take a look at how to actually use it in Python. PyMongo, the official driver released by MongoDB developers, is described in some examples here, but you should also view the complete documentation, because we cannot cover all aspects.
Of course, the first thing is installation. The simplest way ispip
:
pip install pymongo==3.4.0
Note: For a more comprehensive guide, please refer to the installation/upgrade page of the document and follow the steps below to set
After setting, start the Python console and run the following command:
>>> import pymongo
If no exception is raised, the installation is successful.
Establish a connection
UseMongoClient
Object connection:
from pymongo import MongoClientclient = MongoClient()
Use the code snippet above to connect to the default host (localhost) and Port (27017 ). You can also specify the host and/or port:
client = MongoClient('localhost', 27017)
Or use the signed URL format:
client = MongoClient('mongodb://localhost:27017')
Access Database
Once you have a connectionMongoClient
Instance, you can access any database on the Mongo server. If you want to access a database, you can access it as a property:
db = client.pymongo_test
Alternatively, you can use dictionary-based access:
db = client['pymongo_test']
If your specified database has been created, it doesn't matter. By specifying the Database Name and saving the data to it, you will automatically create a database.
Insert document
Storing data in a database is as easy as calling just two lines of code. The first line specifies the set you will use. In terms of MongoDB, a collection is a set of documents (equivalent to SQL tables) stored together in the database ). Collections and documents are similar to SQL tables and rows. The second line is the insert_one () method for inserting data using a set:
posts = db.postspost_data = { 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}result = posts.insert_one(post_data)print('One post: {0}'.format(result.inserted_id))
We can even use insert_one () to insert many documents at the same time. If you have many documents added to the database, you can use insert_ignore (). This method accepts a list parameter:
post_1 = { 'title': 'Python and MongoDB', 'content': 'PyMongo is fun, you guys', 'author': 'Scott'}post_2 = { 'title': 'Virtual Environments', 'content': 'Use virtual environments, you guys', 'author': 'Scott'}post_3 = { 'title': 'Learning Python', 'content': 'Learn Python, it is easy', 'author': 'Bill'}new_result = posts.insert_many([post_1, post_2, post_3])print('Multiple posts: {0}'.format(new_result.inserted_ids))
You should see similar output:
One post: 584d947dea542a13e9ec7ae6Multiple posts: [ ObjectId('584d947dea542a13e9ec7ae7'), ObjectId('584d947dea542a13e9ec7ae8'), ObjectId('584d947dea542a13e9ec7ae9')]
Note: Don't worry. You are different from the one shown above. They are dynamic identifiers consisting of Unix epochs, machine identifiers, and other unique data when inserting data.
Search documents
You can use the find_one () method to retrieve documents. For example, you need to find a record whose author is Bill:
Bills_post = posts. find_one ({'author': 'Bill '}) print (bills_post) running result: {'author': 'Bill', 'title': 'learning python ', 'content': 'learn Python, it is easy', '_ id': ObjectId ('584c4afdea542a766d251_1 ')}
You may have noticed that the ObjectId in this article is the set _ id, which can be used as a unique identifier in the future. To query multiple records, use the find () method:
Scotts_posts = posts. find ({'author': 'Scott '}) print (scotts_posts) Result: <pymongo. cursor. Cursor object at 0x109852f98>
The main difference is that the document data is not directly returned to us as an array. Instead, we get an instance of the cursor object. This Cursor is an iterative object that contains a considerable number of auxiliary methods to help you process data. To obtain each document, you only need to traverse the results:
for post in scotts_posts: print(post)
4. Producer Engine
Although PyMongo is very easy to use, it is a great wheel in general, but many projects can use it too low. In short, you must write a lot of your own code to continuously save, retrieve, and delete objects. On PyMongo, a higher abstraction library is provided. MongoEngine is an object document Er (ODM), which is roughly equivalent to an SQL-based object relation Er (ORM ). The abstraction provided by the engine is class-based, so all models you create are classes. Although there are quite a few Python libraries that can help you use MongoDB, MongoEngine is better because it has a good combination of features, flexibility and community support.
Install using pip:
pip install mongoengine==0.10.7
Connection:
from mongoengine import *connect('mongoengine_test', host='localhost', port=27017)
Different from pymongo. The engine must specify the database name.
Definition document
Before creating a document, you must define fields in the document to store data. Like many other ORM, we will do this by inheriting the Document class and providing the desired data type:
import datetimeclass Post(Document): title = StringField(required=True, max_length=200) content = StringField(required=True) author = StringField(required=True, max_length=50) published = DateTimeField(default=datetime.datetime.now)
In this simple model, we have already told Alibaba engine that our Post instances include title, content, author, and published. Now the Document object can use this information to verify the data we provide.
Therefore, if we try to save the Post without a title, it will throw an Exception to let us know. We can even further exploit this and add more restrictions:
Required: required;
Default: If there are no other values, the specified default value is used.
Unique: Make sure that no other document in the SET has the same value for this field
Choices: Make sure that the value of this field is equal to one of the given values in the array.
Save document
Save the document to the database. We will use the save () method. If the database in the document already exists, all changes will be made to the existing document at the atomic level. If it does not exist, it is created.
Here is an example of creating and saving a document:
post_1 = Post( title='Sample Post', content='Some engaging content', author='Scott')post_1.save() # This will perform an insertprint(post_1.title)post_1.title = 'A Better Post Title'post_1.save() # This will perform an atomic edit on "title"print(post_1.title)
Note the following when calling save:
PyMongo will be called by you. verify when saving (), which means that it will check the data to be saved according to the pattern you declare in the class. If the pattern (or constraint) is violated ), an exception is thrown and data is not saved;
Because Mongo does not support real transactions, there is no way to "Roll Back". save () calls like in the SQL database.
When the data you save has no title:
post_2 = Post(content='Content goes here', author='Michael')post_2.save()raise ValidationError(message, errors=errors)mongoengine.errors.ValidationError:ValidationError (Post:None) (Field is required: ['title'])
Directed Object Features
Using External engine is object-oriented. You can also add methods to your subclass document. For example, in the following example, a function is used to modify the default query set (all objects in the set are returned ). By using it, we can apply the default filter to the class and obtain only the required objects.
class Post(Document): title = StringField() published = BooleanField() @queryset_manager def live_posts(clazz, queryset): return queryset.filter(published=True)
Associate other documents
You can also use the ReferenceField object to create references from one document to another. The consumer engine automatically handles references in an inert manner during access.
class Author(Document): name = StringField()class Post(Document): author = ReferenceField(Author)Post.objects.first().author.name
In the above code, we can easily find the author of the first article by using the "foreign key" of the document. In fact, there are more field classes (and parameters) than described here, so be sure to view more information about the fields in the document.
From all of these examples, you can see that engine is ideal for managing database objects for almost any type of applications. These features make it easy to create an efficient and extensible program. If you are looking