Django + MongoDB

Source: Internet
Author: User
ArticleDirectory
    • Bala nosql & MongoDB
    • Django's support for nosql
    • Django + MongoDB options
    • Django + logging Engine
    • Others
Bala nosql & MongoDB

Currently, nosql does not have a standard definition. The most common explanation is "non-relational". Many people also interpret it as "not only SQL ". Nosql usually means non-relational and distributed data storage, featuring high performance, horizontal scalability, and flexible data models with large data volumes. Maybe it also means open source.

MongoDB is a nosql stored in the form of documents. Its main features:

API: bson
Protocol: Lots of Langs
Query Method: Dynamic Object-based language & mapreduce
Replication: master slave & Auto-sharding
Written in: C ++
Concurrency: update in place
Misc: indexing, gridfs links

Django's support for nosql

Yes, Django will be native to support nosql, but not now.

You may have heard of Django-nonrel. That is, Django none relationship, non-relational Django. Django-nonrel provides an adapter that works with a specific backend to connect to various nosql databases. For example, connect to the Django-MongoDB-engine of MongoDB. However, the implementation of Django-nonrel is to simulate SQL database operations in the memory. This idea is not supported by the Django core, so it can only be an external version.

Query-refactor is one of the Django projects of gsoc (Google summer of code) and has entered the official branch of Django. Alex, author of query-refactor, has already published the "final state" of query-refactor".However, because the completion time of this branch is too close to the release time of django1.3, the query-refactor is not found in the new feature list docs.djangoproject.com-1.4 of django1.4. If there are well-informed people, you may wish to disclose it.

Note: Alex is not a pawn. We know that Django did not support multiple databases before 1.2. It is precisely because of Alex's efforts that we can enjoy Django's multi-database features.

Django + MongoDB options
  1. The most direct method is to use the python driver provided by pymongo-MongoDB. However, I am used to Django's model layer, so I am reluctant to maintain database connections and write a lot of crud statements.
  2. Django-nonrel, as mentioned earlier, may not be a good choice. For the same reason, Django-MongoDB-engine is based on Django-nonrel's MongoDB backend implementation and is not considered.
  3. Engine, which I think is the best choice before Django officially supports. The idea engine provides a very similar experience with Django model (ORM), allowing you to get started quickly. Take a look at the examples on the homepage of the official website:
     From Mongoengine Import * #  To define a schema for  
    # Document, we create
    Class Metadata (embeddeddocument ): # Class that inherits from
    Tags = listfield (stringfield ()) # Document.
    Revisions = listfield (intfield ()) #
    # Fields are specified
    Class Wikipage (document ): # Adding field objects
    Title = stringfield (required = true)# Class attributes to
    TEXT = stringfield () # Document class.
    Metadata = embeddeddocumentfield (metadata) #
    # Querying is achieved
    >>> Page. Title = "Hello, world !" # Calling the objects
    >>> For Page In Wikipage. objects: # Attribute on a document
    >>> Print Page. Title # Class.
Django + logging Engine

First install the runtime engine (dependent on pymongo ). Then, experiment in Python shell:

 From Mongoengine Import *

Connect ( ' Employeedb ' )
Class Employee (document ):
Name = stringfield (max_length = 50)
Age = intfield (required = false)

John = employee (name = " John doe " , Age = 25)
John. Save ()

Jane = employee (name = " Jane Doe " , Age = 27)
Jane. Save ()

For E In Employee. Objects. All ():
Print E [ " ID " ], E [ " Name " ], E [ " Age " ]

It is also easy to use in Django. You only need to write in models. py (if you are very concerned, you can also put it in docs. py separately) as follows:

FromMongoengineImport*
FromMysite. SettingsImportDbname

Connect (dbname)
 
ClassEmployee (document ):
Name = stringfield (max_length = 50)
Age = intfield (required = false)

Dbname is specified in settings. py.

Then, you can use the document model in the view.

Others

1. The field type of Mongo engine is different from that of Django Orm. The following is a simple comparison:

external engine Django
stringfield charfield
urlfield urlfield
emailfield emailfield
intfield integerfield
floatfield floatfield
decimalfield decimalfield
booleanfield booleanfield
datetimefield datetimefield
embeddeddocumentfield --
dictfield --
listfield --
sortedlistfield --
binaryfield --
objectidfield --
filefield filefield

2. Although it looks like an Orm, MongoDB is definitely not an RDB. I think the engine is designed for ease of use, but when using it, you must follow the nosql approach to think about the problem.

 

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.