Mongoengine Action Note one

Source: Internet
Author: User
Tags connect mongo mongodb mongodb collection
mongoengine Action Note one

This paper mainly describes the use of Mongoengine through Python3,flask, the establishment of data model: 1. Installing related libraries

PIP3 install flask, Mongoengine 2. Introduction of Dependency

From flask import flask from
flask_mongoengine import mongoengine
3. Connect MONGO DB

DB is the name of the database for which the connection is requested, the host is the database address, port is the ports used, and the login name and password can be set

App = Flask (__name__)
app.config[' mongodb_settings '] = {'
    db ': ' Test ',
    ' host ': ' 127.0.0.1 ', '
    Port ': 27017,
    ...
}
db = Mongoengine (APP)

There are a number of ways to connect, and no other way to repeat 4. Create a collection connection method

Class Todo (db. Document):
    meta = {
        ' collection ': ' Test_user ',
        ' ordering ': ['-age '],
        ' strict ': False
    }
    # Relationship Mapping
    # task = db. Stringfield ()
    name = db. Stringfield ()

The collection in the meta specifies that the collection,ordering to be manipulated is sorted according to a field ("-" in reverse order), strict defaults to True, False is recommended ... Mode two
Do not declare meta, the class name defaults to the name of MongoDB collection 5. Define Document

For example: MongoDB The data format is as follows

{
    "_id": ObjectId ("595cb0042c4cd79cec85320b"),
    "X": 3.0,
    "name": "Admin3",
    "age": 3.0
}

The corresponding class is:

Class Todo (db. Document):
    meta = {
        ' collection ': ' Test_user ', '
        ordering ': ['-age '],
        ' strict ': False,
    }
    # relational mapping
    name = db. Stringfield () Age
    = db. Floatfield ()
    x = db. Floatfield ()
6. How to declare different types of data 6.1 Types of data supportedBinaryfield Booleanfield Complexdatetimefield datetimefield decimalfield dictfield DynamicField EmailField Embeddeddocumentfield Embeddeddocumentlistfield Filefield Floatfield Genericembeddeddocumentfield Genericreferencefield Genericlazyreferencefield Geopointfield ImageField intfield ListField MapField ObjectIdField Referencefield Lazyreferencefield Sequencefield sortedlistfield stringfield urlfield UUIDField PointField Linestringfield Polygonfield Multipointfield Multilinestringfield Multipolygonfield 6.2 Mode of OperationString (Other simple data type operations are the same)

For example: "Name": "WMH"

Name = db. Stringfield () List

Example 1: "List": [1,2,3,...]

Lists = db. ListField ()
Example 2: "List": [{' Name ': ' A '} ...] list contains the Dict type
Now you need to give birth to a new class.

Class Inner (db. embeddeddocument):
    index = db. Floatfield ()

class Todo (db. Document):
    meta = {
        ' collection ': ' Test_user ',
        ' strict ': False
    }
    # relational mapping
    datas = db. ListField (db. Embeddeddocumentfield (Inner))

Note the use of fields, and other similar operations can be done

Dict
Operation through embeddeddocument operation through the above list of Example 2

Using the Dictfield operation Dict type can cause an error (it's hard to troubleshoot), but it's OK to use embeddeddocument.

Connect two Tables
If Table A and table B are now associated with the same field role (equivalent to the foreign key in MySQL)

Class role (db. Document):
    name = db. Stringfield (required=true)
    description = db. Stringfield ()
class A (db. Document):
    name = db. Stringfield (required=true)
    roles = db. ListField (db. Referencefield (role), default=[], required=true, unique=true)

Class B (db. Document):
    db. Stringfield (required=true)
    roles = db. ListField (db. Referencefield (role), default=[], required=true, unique=true)

This associates its A and B 7 data type Properties according to the Referencefield

For example:

Name = db. Stringfield (max_length=12, min_length=2, required=true,default= ' admin ',)

The field is between 2-12 and the field is required, and the default value is admin.

Refer to official documentation

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.