The application of MongoDB in Python--using Pymongo and Mongoengine

Source: Internet
Author: User

First, the previous article

Djangois aPython Webframework, by object-relational mapper(ORM), a back-end controller, and a template system. MongoDBis a document-oriented database (also known asNoSQLdatabase) to effectively scale and deliver high performance. In this article, we will learn how toPythoncalledMongoDB(usingMongoengine), and how to integrate it intoDjangoproject to replace the built -inORM.

a NoSQL database is a " Next-generation database that has several main points: non-relational, distributed, open source, and horizontally scalable ." Document-oriented database MongoDB is this type of database. It is easy to add support for MongoDB, but at the cost of losing the automatic admin panel. Therefore, you must weigh the tradeoffs according to your needs.

MongoDB is a modeless database, the exact opposite of a relational database. A modeless database does not use a table, but instead uses a collection of documents. These documents are created using the object literal syntax.

Refer to:

http://www.zhihu.com/question/20059632

Http://www.csdn.net/article/2014-03-06/2818652-when-use-mongodb-rather-mysql


Second, the project needs

Django calls MongoDB through the Mongoengine library, The Pymongo Library is used at the bottom of the mongoengine, so be sure to install the Pymongo version of the mongoengine when installing it. Several commands, respectively, for installing PYMONGO,MONGODB, off/on MongoDB services, if the Easy_install installation is unsuccessful, install the Easy_install tool:

Easy_install pymongoapt-get Install Mongodbservice MongoDB stop/start
The first part:

Pymongo

<pre name= "code" class= "HTML" >import Pymongocon=pymongo. Connection (' 127.0.0.1 ', 27017) #连接到数据库db =CON.CIMCC  #获取数据库db. Authenticate (' username ', ' password ') collection= Db.cimcc_user #获得表

Examples of sub-applications:

1. Find

Collection.find ({' Level ': 2, ' userid ': Int (float (item)})

Return to list, below 2 for an example

2, descending sort, get key value

For I in Collection.find (). Sort ([("UserID",-1)]):

userid=i[' userid ']+userid

3. Inserting Data items

Collection.insert ({' UserID ': 1})

4. Update

Collection.update ({' userid ': Int (float (item))},{' $set ': {' status ': 1}},upsert=false,multi=true)

Interested can study the update of the four parameters, the first is a match, the second is the condition (the condition can be a masterpiece article), the third is not found whether to insert, fourth for whether to support multi-action

Pymongo is straightforward, but out of the Python web design, Django is a typical example of MVC design, and then using Pymongo can not reflect this very well, high coupling.


Part II:

Mongoengine

In fact, I was going to use Django-nonrel, because it can be used to simulate ORM in memory, inheriting models. Model, Manager admin has a visual Management database Interface (self), you can also use the model of the function map to MongoDB, but think about it, and later failed to install, estimated by the wall. Later found to know that some people said:

django ORM does not support NoSQL, but there is a branch called Django-nonrel that extends the Django Orm and supports some NoSQL databases, including MongoDB

from the last time I cared about Django-nonrel, the usability of the actual production environment was very difficult to use. For example,

    1. MongoDB class NoSQL does not have a join operation, so there is no multiple table inheritance function when using MongoDB in Django-nonrel, only from the abstract class
    2. No join operation also means that many atomic SQL queries need to be converted to non-atomic NoSQL queries
    3. Many of the original ORM features are used in Django-nonrel to throw notimplemented exceptions.
so don't use the NoSQL support provided by Django-nonrel under normal circumstances (unless you are interested in contributing code to it)

http://www.zhihu.com/question/19818326

So I still use mongoengine, inherit document instead of Models.model, the effect is also OK.

From Mongoengine import *connect (' database ', host= ' 127.0.0.1 ', username= ' username ', password= ' password ') # Create your Models Here.class Cimc_message (Document): Msg_from=stringfield (required=true) To=intfield (required=true) msg_type= Stringfield (required=true) Status=intfield (required=true) Result=intfield (required=true) def insert (self): Self.save ()

1, Get_or_create

Cimc_message.objects.get_or_create (msg_from= ' admin ', to= ' xiaoming ', msg_type= ' type1 ', status=0,defaults=msg_dic)

Return the data if a match is found, or insert defaults,defaults as a dictionary

2. Delete

Cimc_message.objects (msg_from= ' admin ', to= ' xiaoming ', msg_type= ' type1 ', status=0). Delete ()

... Other extrapolate, use the side to find




The application of MongoDB in Python--using Pymongo and Mongoengine

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.