Pymongo to manipulate the MongoDB database, but writes the operation code of the database directly in the script, which makes the application's code more coupled, and is not conducive to the optimization of the management of the Code
The general application is designed using the MVC framework, in order to better maintain the MVC structure, we need to pull out the database operation part as model, which requires the help of Mongoengine
Mongoengine is an Object document Mapper (ODM), equivalent to an SQL-based object-relational mapper (ORM)
The abstraction provided by Mongoengine is class-based, and all models created are classes
Installation
Pip Install Mongoengine
Declare a class that inherits from Mongoengine.document first when used
Declaring a property in a class is equivalent to creating a data structure to hold the data, that is, the data has been stored in a database in the form of a similar structure, usually by storing such classes in a script as the model module of the application
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) Users= Users.objects.all ()#returns a list of all document objects forUinchUsers:Print("Name:", U.name,", Age:", U.age)
Save document
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) User1=Users (Name='ZZ', Age= 11) User1.save ()Print(user1.name) user1.name='Zz11'User1.save ()Print(User1.name)
Query 10=< age <30, by name
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) User_search= Users.objects (age__gte=10, age__lt=33). Order_by ('name') forUinchUser_search:Print("Name:", U.name,", Age:", U.age)
Query 10=< age <30, reverse by name
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) User_search= Users.objects (age__gte=10, age__lt=33). Order_by ('-name') forUinchUser_search:Print("Name:", U.name,", Age:", U.age)
Query Name=zz11
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) TMP= Users.objects (name="Zz11") forUinchtmp:Print("Name:", U.name,", Age:", U.age)
Modify the Age plus 1 for Name=zz11
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) TMP= Users.objects (name="Zz11"). Update (inc__age=1) TMP= Users.objects (name="Zz11") forUinchtmp:Print("Name:", U.name,", Age:", U.age)
Modify the age of Name=zz11 to 55
fromMongoengineImport*Connect ('Test', host='localhost', port=27017)ImportdatetimeclassUsers (Document): Name= Stringfield (Required=true, max_length=200) Age= Intfield (required=True) TMP= Users.objects (name="Zz11"). Update (set__age=55) TMP= Users.objects (name="Zz11") forUinchtmp:Print("Name:", U.name,", Age:", U.age)
Using MongoEngine1 in Python