Python MongoDB Usage

Source: Internet
Author: User
Tags mongoclient mongodb like

MongoDB Introduction

MongoDB is a document-oriented, open-source database program that is platform Independent. MongoDB like some other NoSQL database (but not all!) Documents that use a JSON structure store data. This is the schema that makes the data very flexible and unwanted.

Some of the more important features are:

    • supports a variety of standard query types, such as matching (), comparison (,), or regular expressions;
    • can store almost any type of data, whether structured, partially structured, or even polymorphic;
    • To expand and process more queries, simply add more machines;
    • It is highly flexible and agile, allowing you to quickly develop applications;
    • Being a document-based database means that you can store all the information about your model in a single document;
    • You can change the schema of the database at any time;
    • The functionality of many relational databases can also be used in MongoDB (such as indexing).

In terms of operation, there are quite a few features in MongoDB that are not available in other databases:

    • Whether you need a standalone server or a complete standalone server cluster, MongoDB can scale as needed;
    • MongoDB also provides load balancing support by automatically moving data on individual shards;
    • It has automatic failover support, if the primary server down, the new master server will automatically start and run;
    • MongoDB's Management Services (MMS) can be used to monitor and back up MONGODB infrastructure services;
    • Unlike relational databases, you will save quite a lot of RAM due to memory-mapped files.

Although at first MongoDB seems to be a database to solve many of our problems, it is not without drawbacks. A common drawback of mongodb is the lack of support for acid transactions, and MongoDB supports acid transactions in specific scenarios, but not in all cases. At the single document level, acid transactions are supported (this is where most transactions occur). However, because of the distributed nature of MongoDB, transactions that process multiple documents are not supported.

MongoDB also lacks support for natural join queries. In MongoDB's view: Documents are intended to be all-encompassing, which means that, in general, they do not need to refer to 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 supplemental database for a SQL database, but when you use MongoDB Yes, you will find that this is wrong.

Pymongo

The official driver Pymongo (https://pypi.python.org/pypi/pymongo/), released by the MongoDB developer, is presented here through some examples, but you should also review the full documentation (https:// api.mongodb.com/python/current/).

1, pip mode installation

Pip Install pymongo==3.4.0

2. Module reference

Import Pymongo

3. Establish the connection

Connect using the Mongoclient object: from Pymongo import mongoclientclient = Mongoclient () using the code snippet above, the connection to the default host (localhost) and port (27017) will be established. You can also specify the host and/or use the port: client = mongoclient (' localhost ', 27017) or use Mongourl format: client = mongoclient (' Mongodb://localhost : 27017 ')

4. Accessing the database

Once you have a connected mongoclient instance, you can access any database in the MONGO server. If you want to access a database, you can access it as a property: db = Client.pymongo_test or you can access it in dictionary form: db = client[' pymongo_test ']

If your specified database is already created, it doesn't really matter. By specifying this database name and saving the data to it, you will automatically create the database.

5. Inserting documents

Storing data in a database is as easy as calling just two lines of code. The first line specifies which collection you will use. In MongoDB terminology, a collection is a set of documents that are stored together in a database (the equivalent of a table of SQL). Collections and documents are similar to SQL tables and rows. The second line is to use the collection to insert the data Insert_one () method:

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, and if you have a lot of documents to add to the database, you can use Method Insert_many (). This method accepts a list parameter:

post_1 = {    ' title ': ' Python and MongoDB ',    ' content ': ' Pymongo is fun, you guys ',    ' author ': ' Scott '}post_2 = { c3/> ' title ': ' Virtual environments ',    ' content ': ' Use of virtual environments, you guys ',    ' author ': ' Scott '}post_3 = {    ' title ': ' Learning python ',    ' content ': ' Learn python, it's easy ',    ' author ': ' Bill '}new_result = posts.in Sert_many ([Post_1, Post_2, Post_3]) print (' multiple posts: {0} '. Format (new_result.inserted_ids))

See Similar output:

One post:584d947dea542a13e9ec7ae6multiple posts: [    ObjectId (' 584d947dea542a13e9ec7ae7 '),    ObjectId (' 584d947dea542a13e9ec7ae8 '),    ObjectId (' 584d947dea542a13e9ec7ae9 ')]

Note: do not worry, you are not the same as shown above. They are dynamic identities that are made up of the Unix era, machine identifiers, and other unique data when inserting data.

6. Retrieving Documents

Retrieving a document can use the Find_one () method, for example to find a record of author as Bill:

Bills_post = Posts.find_one ({' Author ': ' Bill '}) print (bills_post) Run result: {    ' author ': ' Bill ',    ' title ': ' Learning Python ',    ' content ': ' Learn python, it's easy ',    ' _id ': ObjectId (' 584c4afdea542a766d254241 ')}

The Objectid of this article is to set the _id, which is a unique identity that can be used later. If you need to query multiple records, you can use the Find () method:

Scotts_posts = Posts.find ({' Author ': ' Scott '}) print (scotts_posts) Result: <pymongo.cursor.cursor object at 0x109852f98 >

His main difference is that the document data is not returned directly to us as an array. Instead, we get an instance of a cursor object. This cursor is an iterative object that contains quite a few helper methods to help you work with the data. To get each document, simply iterate through the results:

For post in scotts_posts:    print (POST)

Mongoengine

while Pymongo is very easy to use, overall it is a great wheel, but many projects using it may be too low level. In short, you have to write a lot of your own code to persist, retrieve, and delete objects. Pymongo provides a higher abstraction of a library that is mongoengine. Mongoengine is an Object document Mapper (ODM), which is roughly equivalent to an SQL-based object-relational mapper (ORM). The abstraction provided by Mongoengine is class-based, so all the models you create are classes. While there are quite a lot of Python libraries that can help you use Mongodb,mongoengine is a better one because it has a good combination of functionality, flexibility and community support .

User guide: http://docs.mongoengine.org/guide/index.html

1, pip mode installation

Pip Install mongoengine==0.10.7

2. Connection

From Mongoengine import *connect (' Mongoengine_test ', host= ' localhost ', port=27017)

remark: different from Pymongo. Mongoengine the database name needs to be established.

3. Define the document

Before you create a document, you need to define the fields in your document that you want to hold data. Similar to many other Orm, we will do this by inheriting the document class and providing the type of data we want:

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 told Mongoengine that our post instance has title, content, author, published. Now the Document object can use this information to verify the data that we provide it.

So if we try to save the post without the title then it will throw a exception and let us know. We can even make further use of this and add more restrictions:

    • Required: set must;
    • Default: If no other value is given using the specified defaults
    • Unique: Ensure that no other document in the collection has the same value for this field
    • Choices: Ensure that the value of this field is equal to one of the given values in the array

4. Save the document

Save the document to the database and we will use the Save () method. If the database already exists in the document, all changes will be made to the existing document at the atomic level. If it does not exist, however, then it will be 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 would perform an insertprint (post_1.title) post_1.title = ' A Better post title ' Post_1.save ()       # This would perform an Atomic edit on "title" Print (Post_1.title)

There are a few things to note when calling Save ():

    • Pymongo performs validation when you call. Save (), which means that it checks the data to be saved based on the pattern you declare in the class, throws an exception if the pattern (or constraint) is violated, and does not save the data;
    • Because MONGO does not support true transactions, there is no way to "roll back" as in SQL database. Save () call.

When your saved data does not have a 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 '])

5, the characteristics of the object

Using Mongoengine is Object-oriented, you can also add a method to your subclass document. For example, the following example, where a function modifies the default query set (all objects that return a collection). By using it, we can apply a default filter to the class and get only the desired object

Class Post (Document):    title = Stringfield ()    published = Booleanfield ()    @queryset_manager    def live_ Posts (Clazz, Queryset):        return Queryset.filter (published=true)

6. Associating other documents

You can also use the Referencefield object to create a reference from one document to another. Mongoengine automatically processes references on access.

Class Author (document):    name = Stringfield () class Post (document):    Author = Referencefield (Author) Post.objects.first (). Author.name

In the above code, using the document "foreign key", we can easily find the author of the first article. There are more field classes (and parameters) than are described here, so be sure to check the document fields for more information.

Reference Blog: https://realpython.com/blog/python/introduction-to-mongodb-and-python/

https://my.oschina.net/jhao104/blog/812002

Python MongoDB Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.