This article will give you an in-depth explanation of MongoEngine classic query [detailed case included]

Preface

In order to consolidate the knowledge learned, the author tried to start publishing some study note blogs to facilitate future review. Of course, it would also be great if it could help some newbies learn new technologies. The author is a rookie. If there are recording errors in the article, readers and friends are welcome to criticize and correct them.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

discover treasure

A few days ago I discovered a giant artificial intelligence learning website. It is easy to understand and humorous. I couldn’t help but share it with everyone. 【Treasure Entrance】.

1. Query database (all)

  • In MongoEngine, to query all documents in the MongoDB database, you canuse the objects attribute of the model and optionally call the all() method.
books = Books.objects
books2 =  Books.objects.all()

Insert image description here

  • The Document class has an objects attribute that is used to access objects in the database associated with the class. The objects property is actually a QuerySetManager, which creates and returns a new QuerySet object when accessed >. This QuerySet object can be iterated to obtain documents from the database:
for book in Books.objects:
    print (book.bookName)

Insert image description here

2. Filter query (single, condition)

1. Check a single item

In MongoEngine, to query a single document, you can use the first() method or get () method, depending on your needs and query conditions.

1. Use the first() method to query the first document that meets the conditions

# 查询第一个 brand 字段名称为 rmzxb 的报纸
books = Books.objects(brand='rmzxb').first()

Insert image description here

2. Use the get() method to query the unique document that meets the criteria (if it exists)

  • To retrieve results that should be unique in the collection, use get() . DoesNotExist This value is raised if there are no documents matching the query and MultipleObjectsReturned has more than one document matching the query. These exceptions will be merged into your document definition, for example: MyDoc.DoesNotExists

  • A variant of this method get_or_create() exists, but it is unsafe. It cannot be secured because there are no transactions in mongoDB. Other methods should be investigated to ensure that data is not accidentally copied when using methods similar to this one. Therefore, it was deprecated in 0.8 and removed in 0.10.

  • Query success example

# 查询符合条件的唯一文档(如果存在)
books = Books.objects.get(isbn='4754959145206026241')

Insert image description here

  • Query failure example
# 查询符合条件的唯一文档(如果存在)
books = Books.objects.get(brand='rmzxb')

Insert image description here

2. Available filter query operators

effect symbolic representation Full English name
not equal to it is not equal to
less than lt less than
less than or equal to lte less than or equal to
more than the gt greater than
greater than or equal to gte greater than or equal to
Negative standard check, can be used before other operators, for example, query documents whose age is not a multiple of 5: Q (age_ not _mod =(5, 0)) not
value is in list (list of values ​​should be provided) in
Value is not in list (list of values ​​should be provided) of
Query documents whose age field satisfies the modulus condition. For example, documents whose age is divided by 5 and have a remainder equal to 1 result = Person.objects(age_ _mod=(5, 1)) against
Each item in the provided list of values ​​is in an array, for example using the all method to query for people with all specified hobbies people = Person.objects(hobbies_ _all=hobbies) all
The size of the array, for example using the size method to query people with a specific number of hobbies count = Person.objects(hobbies_ _size=hobby) size
The field value exists, for example, use the exists() method to query the document containing the name field people_with_name = Person.objects(name_ _exists=True) exists

3. Demonstration of using filter query operators

  • Search newspapers with a date less than 2023-05-25
presentPublishDate = '2023-05-25T00:00:00.000+00:00'
book = Books.objects(publishDate__exists=True, publishDate__lt=presentPublishDate)

Insert image description here

  • Query isbn for newspapers in the list
isbns = ['4759307800083959809','4760032575949377537']
book = Books.objects(isbn__in=isbns)

Insert image description here

3. String query (fuzzy)

1. Available fuzzy query operators

effect Character representation
string field exact match value exact
String fields match values ​​exactly (case insensitive) iexact
string field contains value contains
String fields contain values ​​(case insensitive) icontains
String fields start with value startswith
String fields start with value (case insensitive) istartswith
String fields end with value endswith
String fields end with a value (case insensitive) iendswith
String fields contain entire words wholeword
String fields contain whole words (case insensitive) iwholeword
Regular expression string field matching regex
Regular expression string field matching (case insensitive) iregex
Do $elemMatch so you can match the entire document in the array match

2. Demonstration of using fuzzy query operators

  • Query newspapers whose article titles contain a specified string
title_input = '回信勉励海军'
article = Cards.objects(title__contains=title_input).first()

4. Original query ( _ _ raw _ _ )

1. Detailed explanation of original query

  • In MongoEngine, you can use raw queries to execute MongoDB query statements, which allows you to directly send MongoDB query commands without being restricted by the MongoEngine API. Raw queries are useful when you need to perform complex or specialized queries because they allow you to write MongoDB queries directly.

  • can provide the original PyMongo query as a query parameter, which will be integrated directly into the query. This is done using the raw keyword argument:

  Page.objects(__raw__={
    
    'tags': 'coding'})
  • Likewise, raw updates can be provided to the method update() :
  Page.objects(tags='coding').update(__raw__={
    
    '$set': {
    
    'tags': 'coding'}})
  • And the two can also be combined:
  Page.objects(__raw__={
    
    'tags': 'coding'}).update(__raw__={
    
    '$set': {
    
    'tags': 'coding'}})
  • Note that raw queries allow you to perform complex queries, but also need to be used with caution as they bypass MongoEngine's object mapping and validation. Make sure your original query is correct and secure to avoid potential security issues.

2. Original query example

  • Query newspapers whose id is within the specified range and whose snapshots field value is not 0
book_ids = [13586,13849,13871]
sm = {
    
    '_id': {
    
    '$in': book_ids}, 'snapshots.0': {
    
    '$exists': True}}
docs: Books = Books.objects(__raw__=sm)

Insert image description here

5. Sorting/sorting results (+, -)

You can use order_by() to sort the results by 1 or more keys. The order can be specified by preceding each key with "+" or "-". If there is no prefix, ascending order is assumed.

# Order by ascending date
blogs = BlogPost.objects().order_by('date')    # equivalent to .order_by('+date')

# Order by ascending date first, then descending title
blogs = BlogPost.objects().order_by('+date', '-title')

6. Limit and skip results ([ ], first())

  • Like traditional ORMs, we can limit the number of results returned or skip numbers or results in the query. The limit() and skip() methods are available on QuerySet objects, but the array slicing syntax is preferred for this purpose:
# Only the first 5 people
users = User.objects[:5]

# All except for the first 5 people
users = User.objects[5:]

# 5 users, starting from the 11th user found
users = User.objects[10:15]
  • Queries can also be indexed to retrieve individual results. Will be raised if the item in this index does not exist. IndexError provides a shortcut to retrieve the first result and return None if no result exists ( first() ):
# Make sure there are no users
>>>User.drop_collection()
>>>User.objects[0]
IndexError: list index out of range
>>>User.objects.first() == None
True
>>>User(name='Test User').save()
>>>User.objects[0] == User.objects.first()
True

7. Aggregation query

1. Sum

  • We can sum the values ​​of a specific field on a document using sum() :
  yearly_expense = Employee.objects.sum('salary')
  • If the field does not exist in the document, the document will be omitted from the sum.

2. Find the average

  • To get the average of a field on a collection of documents, use average() :
  mean_age = User.objects.average('age')

3. Counting

  • Just like limiting and skipping results, there is a method on the QuerySet object – count() :
  num_users = User.objects.count()
  • Technically, we could use len(User.objects) to get the same result, but it would be much slower than count() . When you perform a server-side count query, you let MongoDB do the heavy lifting and receive a single integer over the network. At the same time, all results are retrieved, placed in the local cache and len() finally counts them. If we compare the performance of these two operations, len() is much slower than count()

  • Example

book_ids = [13586,13849,13871]
sm = {
    
    '_id': {
    
    '$in': book_ids}, 'snapshots.0': {
    
    '$exists': True}}
count = client.get_db()['books'].count(sm)

Insert image description here

4. Find the frequency

Since MongoDB provides native lists, MongoEngine provides a helper method to get a dictionary of the frequencies of items in a list across the entire collection - item_frequencies() . An example of its use is to generate a "tag cloud":

class Article(Document):
    tag = ListField(StringField())

# After adding some tagged articles...
tag_freqs = Article.objects.item_frequencies('tag', normalize=True)

from operator import itemgetter
top_tags = sorted(tag_freqs.items(), key=itemgetter(1), reverse=True)[:10]

Summarize

You are welcome to leave messages, exchange comments and make criticisms. If the article is helpful to you or you think the author's writing is good, you can click to follow, like, collect and support it.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/133673116