using the mongoengine operation in Python mongodb tutorial
This article mainly introduces the use of Mongoengine operations in Python MongoDB tutorial, including some of the use of Django under the skills, the need for friends can refer to the
Recently picked up Django, but Django does not support MongoDB, but there is a module mongoengine can implement the Django model similar encapsulation. But Mongoengine's Chinese documents are almost never Some is also a brief introduction and use. I'll share some of the notes I've recorded in the course of my use, which may be a bit messy. You can refer to it.
Install Mongoengine
|
Easy_install Pymongo # Dependent library Easy_install Mongoengine |
Basic use
|
From mongoengine Import * from datetime import datetime # Connect database Connect (' blog ') # Connect to local blog database # to verify and specify host name # Connect (' Blog ', host= ' 192.168.3.1 ', username= ' root ', password= ' 1234 ') # define Category document class Categories (document): ' Inherit the document class, for normal document ' name = Stringfield (max_length=30, required=true) Artnum = Intfield (default=0, required=true) date = Datetimefield (default= DateTime.Now (), required=true) |
It's similar to the Django model, so it doesn't explain anything.
Insert
|
Cate = Categories (name= "Linux") # If required is true, you must give the initial value, and if there is default, give the initial value to use the Defaults cate.save () # to save to the database |
Querying and updating
The document class has a objects property. We use it to query the database.
|
# Returns a list of all document objects in the collection cate = Categories.objects.all () # Returns a list of all document objects that match the results of the query cate = categories.objects (name= "Python") # Update query Documents to: Cate.name = "Linuxzen" Cate.update () |
The query array default query array "=" means in:
|
Class Posts (Document): ArtID = Intfield (required=true) title = Stringfield (max_length=100, required=true) content = Strin Gfield (required=true) Author = Referencefield (User) tags = ListField (Stringfield (max_length=20, Required=true), Required=true) categories = Referencefield (categories), required=true) comments = Intfield (default=0, required=true) # will will return all tags containing coding documents posts.objects (tags= ' coding ') |
Referencefield Reference field:
By referencing a field, you can get directly to the document referenced by the Reference field through the document:
|
Class Categories (Document): name = Stringfield (max_length=30, required=true) Artnum = Intfield (default=0, Required=true Date = Datetimefield (Default=datetime.now (), Required=true) class Posts (Document): title = Stringfield (max_length=10 0, required=true) content = Stringfield (required=true) tags = ListField (Stringfield (max_length=20, Required=true), Required=true) categories = Referencefield (categories) |
Insert Reference Field
|
Cate =categories (name= "Linux") Cate.save () post = Posts (title= "linuxzen.com", content= "linuxzen.com", tags=["Linux", " Web "], categories=cate) Post.save () |
To get a reference document object directly from a reference field
A generic document query returns a list (although there is only one result), and we want to get a document object that can use the index to get the first document object, but Mongoengine recommends using first () to get the number one:
|
>>> cate = Posts.objects.all (). A (). Categories >>> Cate >>> cate.name |
U ' Linux '
Query for articles that contain Linux classifications
|
>>> cate = categories.objects (name= "Linux"). A () >>> posts.objects (categories=cate) |
Embeddeddocument Embedded Documents
An inherited Embeddeddocument document class is an embedded document that embeds a document in a Embeddeddocumentfield field that embeds other documents, such as the tags field in the example above, which can be changed to the posts document class if it is converted to an embedded document:
|
Class Posts (Document): title = Stringfield (max_length=100, required=true) content = Stringfield (required=true) tags = Li Stfield (Embeddeddocumentfield (' Tags ') required=true) categories = Referencefield (categories) |
You also need to add a tags embedded document class:
|
Class Tags (embeddeddocument): name = Stringfield () date = Datetimefield (Default=datetime.now ()) |
We insert tags in the posts document as follows
|
>>> tag = Tags (name= "Linuxzen") >>> post = Posts (title= "linuxzen.com", content= "linuxzen.com", tags=[ Tag], categories=cate >>> tag = Tags (name= "MySite") >>> post.tags.append (tag) >>> Post.save () >>> tags = post.tags >>> for tag in tags:print tag.name Linuxzen MySite |
Time period Query
|
Start = datetime (int (year), Int (month), 1) if INT (month) + 1 > 12:emonth = 1 eyear = Int (year) + 1 else:emonth = Int ( Month) + 1 eyear = Int (year) end = DateTime (Eyear, Emonth, 1) articles = Posts.objects (Date__gte=start, date__lt=end). Orde R_by ('-date ') |
Sharding
Slice for slicing
|
# comments-skip 5, Limit Page.objects.fields (Slice__comments=[5, 10]) # can also use index value fragmentation # limit 5 users = user.objects [: 5] # skip 5 users = user.objects[5:] # Skip, limit users = user.objects[10:15] |
Query with original statement
If you want to use the original Pymongo query method, you can use the coding and $INC operators using the __raw__ operator page.objects (raw={' tags ': ' $set '})
|
# Update embedded document Comments field by the value of Joe's document field votes increased by 1 page.objects (comments_by= "Joe"). Update (Inc__votes=1) # Update the embedded document comments The document field votes with Joe is set to 1 page.objects (comments_by= "Joe"). Update (Set__votes=1) Other tips #查询结果转换成字典 Users_ Dict = User.objects (). To_mongo () # sorted by Date user = User.objects.order_by (' Date ') # by date Reverse user = User.objects.order_b Y ("-date") |