SoFunction
Updated on 2024-11-13

Using MongoEngine to manipulate databases in Python tutorial example

This article introduces the use of MongoEngine in Python to operate the database tutorial examples, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference value of learning, the need for friends can refer to the following

pymongo to operate the MongoDB database, but directly to the database operation code are written in the script, which will make the application code coupling is too strong, and is not conducive to the optimization of the code management

General applications are designed using the MVC framework, in order to better maintain the MVC structure, the database operation part of the need to be extracted as a model, which requires the use of MongoEngine

MongoEngine is an Object Document Mapper (ODM), which is equivalent to a SQL-based Object-Relational Mapper (ORM)

The abstraction provided by MongoEngine is class-based, and all models created are classes

mounting

pip install mongoengine

To use it, declare a class that inherits from

Declare some properties in the class, which is equivalent to create a data structure used to store data, that is, the data has been similar to the form of data structures stored in the database, usually such a number of classes are stored in a script, as the application of the Model module

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)

users = () # Returns a list of all document objects
for u in users:
  print("name:",,",age:",)

Save file

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)
user1 = Users(
  name='zz',
  age= 11
)
()  
print()
 = 'zz11'
()    
print()

Query 10=<age<30 by name

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)
user_search = (age__gte=10, age__lt=33).order_by('name')
for u in user_search:
  print("name:",,",age:",)

Query 10=<age<30 in reverse order by name

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)
user_search = (age__gte=10, age__lt=33).order_by('-name')
for u in user_search:
  print("name:",,",age:",)

Query name=zz11

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)

tmp = (name="zz11")
for u in tmp:
  print("name:",,",age:",)

Modify the age of name=zz11 by adding 1.

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)
tmp = (name="zz11").update(inc__age=1)
tmp = (name="zz11")
for u in tmp:
  print("name:",,",age:",)

Change the age of name=zz11 to 55.

from mongoengine import *
connect('test', host='localhost', port=27017)
import datetime
class Users(Document):
  name = StringField(required=True, max_length=200)
  age = IntField(required=True)

tmp = (name="zz11").update(set__age=55)
tmp = (name="zz11")
for u in tmp:
  print("name:",,",age:",)

This is the whole content of this article.