Sling Academy
Home/Python/MongoEngine: Working with ListField

MongoEngine: Working with ListField

Last updated: February 12, 2024

Overview

MongoEngine, a Document-Object Mapper (DOM) for working with MongoDB from Python, offers extensive functionality for managing collections and documents. One of the versatile fields provided by MongoEngine for modeling arrays in documents is the ListField. This tutorial delves into the usage of ListField, from basic to advanced examples, equipping you with the knowledge to effectively incorporate lists into your MongoDB documents.

Introduction to ListField

A ListField in MongoEngine is used to define a list of items in a MongoDB document, where the items can be of any data type, including other documents. It’s a powerful way to store variable-sized arrays of data within a single document. The basic syntax for declaring a ListField is as simple as specifying the field type of the elements in the list.

from mongoengine import Document, ListField, StringField, IntField

class User(Document):
    hobbies = ListField(StringField())
    scores = ListField(IntField())

In this basic example, we’ve defined a User document with two ListFields: one for hobbies (a list of strings) and one for scores (a list of integers).

Adding Items to a ListField

To add items to a ListField, you simply use the append method or other list methods like extend. However, to save the changes to the database, you must call the save method on the document instance.

user = User().save()
user.hobbies.append('Reading')
user.scores.extend([95, 85, 75])
user.save()

The above code adds ‘Reading’ to the hobbies list and three scores to the scores list. Don’t forget to save the document to commit these changes to your database.

Querying Documents with ListField

You can query documents based on conditions that involve ListField elements. MongoEngine provides a way to query for documents where the list contains certain elements, or matches specific conditions.

users_with_reading_hobby = User.objects(hobbies__in=['Reading'])
users_with_high_score = User.objects(scores__gt=90)

The first query retrieves users who have ‘Reading’ as one of their hobbies, while the second query fetches users whose scores list contains a score greater than 90.

Advanced ListField Operations

Beyond basic storage and querying, ListField can also be employed for more complex operations such as embedding documents, enforcing data uniqueness, and using non-primitive types as list items.

Embedding Documents in a ListField

You can store documents within a ListField, enabling rich, nested structures within a single MongoDB document. This is done by specifying a document class in the ListField declaration.

class Address(Document):
    city = StringField()
    country = StringField()

class Person(Document):
    name = StringField()
    addresses = ListField(EmbeddedDocumentField(Address))

This model represents a person with multiple addresses. Each address is modeled as a separate document and embedded within the main person document through a ListField.

Ensuring Uniqueness in a ListField

To ensure that items in a ListField are unique, MongoEngine offers the unique parameter. However, it’s important to note that this feature requires careful consideration of document structure and use case.

class TaggedPost(Document):
    tags = ListField(StringField(), unique=True)

With the unique parameter set to True, the tags list in each TaggedPost document will not accept duplicate values.

Dealing with Complex Data Types

Finally, ListField can also handle more complex data types like dictionaries or even other lists. This ability is particularly useful for storing complex data structures directly in your documents.

class Recipe(Document):
    name = StringField()
    ingredients = ListField(DictField())

In this model, each Recipe document can contain a list of ingredients, where each ingredient is represented as a dictionary with its own set of key-value pairs.

Conclusion

Through its ListField, MongoEngine provides a robust mechanism for modeling list-like structures within MongoDB documents. From storing simple data types to embedding complex documents, ListField can dramatically simplify and enhance data modeling within your applications. By mastering its usage, you can unlock the full potential of document-oriented database design in MongoDB.

Next Article: MongoEngine – Working with EnumField

Previous Article: MongoEngine: How to define document’s schema

Series: Data Persistence in Python – Tutorials & Examples

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types