Engine tutorial (3) Data Query

Source: Internet
Author: User
Like Django, the Document class has an objects attribute. It is used to associate a class with a database. The objects attribute is a QuerySetManager object. Its operations return a QuerySet object. You can obtain data in the database through iteration of the QuerySet object. ClassUser (Document): nameStri

Like Django, the Document class has an objects attribute. It is used to associate a class with a database. The objects attribute is a QuerySetManager object. Its operations return a QuerySet object. You can obtain data in the database through iteration of the QuerySet object. Class User (Document): name = Stri

Like Django, the Document class has an objects attribute. It is used to associate a class with a database. The objects attribute is a QuerySetManager object. Its operations return a QuerySet object. You can obtain data in the database through iteration of the QuerySet object.

class User(Document):    name = StringField()    country = StringField()class Paper(Document):    content = StringField()    author = ReferenceField(User)
Query Filtering

You can specify a filter condition in the query to obtain the desired result. For example, users who want to query the UK:

uk_users = User.objects(country='uk')

Similar to Django, to query referenced objects, you only need to use double underscores. For example:

uk_papers = Paper.objects(author__country='uk')
Query operations

Similar to Django, condition engine also provides some conditional statements.

  • Ne-not equal
  • Lt-less
  • Lte-less than or equal
  • Gt-greater
  • Gte-greater than or equal
  • Not-Inverse
  • In-value in the list
  • Nin-value is not in the list
  • Mod-modulo
  • All-same as the List Value
  • Size-size of the array
  • Exists-the value of the field exists.

For example, to query users younger than or equal to 18 years old:

young_users = Users.objects(age__lte=18)

Different conditional statements are provided for different types of data.

Query Result count limit

Like traditional ORM, the indexing engine can also limit the number of query results. One method is to call the limit and skip methods on the QuerySet object, and the other is to use the array sharding syntax. For example:

users = User.objects[10:15]users = User.objects.skip(10).limit(5)
Aggregate operations

Engine provides aggregation operations for some databases.

You can use the count method of QuerySet or the Python method to calculate the number of statistical results:

num_users = len(User.objects)num_users = User.objects.count()

Other aggregate operations.
Sum:

yearly_expense = Employee.objects.sum('salary')

Average:

mean_age = User.objects.average('age')
Advanced Query

Sometimes multiple conditions need to be combined, and the method mentioned above cannot meet the requirements. In this case, the Q class of consumer engine can be used. It can perform the & (and) and | (OR) operations on multiple query conditions.

For example, the following statement is used to query all British users whose age is greater than or equal to 18 years old, or all users whose age is greater than or equal to 20 years old.

User.objects((Q(country='uk') & Q(age__gte=18)) | Q(age__gte=20))
Execute javascript code on the server side

The exec_js method of the QuerySet object of the runtime engine can send javascript code as a string to the server for execution, and then return the execution result.

For example, query the databases with the following collections:

User.objects.exec_js("db.getCollectionNames()")

Original article address: mongoengine tutorial (3) -- Data Query. Thank you for sharing it with the original author.

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.