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