The method of using ORM (Mongoengine) to control MongoDB in Python __python

Source: Internet
Author: User
Tags mongodb

Above: http://blog.csdn.net/u013205877/article/details/75957573
Pymongo is a low-level MongoDB python drive (which I call a client) that encapsulates the MongoDB API and communicates with MongoDB via JSON, Pymongo maps MongoDB data into Python's built-in type.
Mongoengine is a document-object Mapper (think about ORM, but it's a document-type database) that Python interacts with MongoDB. You might say that Pymongo is also ORM, in Python Everything is an object, but what we call the Orm object refers to the custom class in Python, not the built-in type. Mongoengine or Mongokit maps MongoDB data into custom class instances, all based on Pymongo.
We can compare it to the Python client mysqldb of relational databases, as well as ORM Sqlalchemy/django ORM, Pymongo equivalent to Mysqldb,mongoengine SQLAlchemy, The SQLAlchemy is based on MySQLdb, and the Mongoengine is based on Pymongo.
Mongoengine is an ORM installation

Pip Install Mongoengine
Connection

From mongoengine Import *
Connect (' mongoengine_test ', host= ' localhost ', port=27017) defines the fields in the document that hold the data, and inherits the documents class, where you define the model limits, which you need to do later on save

Import datetime

class 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)

Required: set must;

Default: If no other value is given to use the specified default value

Unique: Make sure no other document in the collection 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

4. Complete Insert-save process

From mongoengine Import *

connect (' mongoengine_test ', host= ' localhost ', port=27017)

import datetime

Class 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)

post_1 = post (
    title= ' Sample post ',
    content= ' Some engaging ',
    author= ' Scott '
)
post_1.save ()
print (post_1.title)

post_1.title = ' agronomy '

post_1.save ()
Print (Post_1.title)

The following are "attributes to Objects" and "associated with other documents" as well as more features, advanced content to see below: http://docs.mongoengine.org/guide/index.html

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.