Course contentsShow
← Databases & SQL
Lesson 1271 of 1,95311. NoSQL and Document ModelsFree lesson

Implementing Access Lists

Design document-level security arrays for fine-grained permissions.

In this lesson, you will learn how to secure individual documents by embedding access control lists (ACLs) directly into your data model.

When working with document-oriented databases, you often need to restrict access to specific records based on user roles or ownership. Instead of relying solely on external middleware, you can embed an access_list array directly into each document. This allows your database queries to naturally filter results based on the current user's identity.

The ACL Pattern

An access list is typically an array of objects or strings within a document that defines who has permission to view, edit, or delete that specific item. By placing these lists inside the document, you keep your security logic tightly coupled with your data, ensuring that access checks are fast and highly granular.

Consider a project management application where a document represents a "Task." You might model the access like this:

{
  "task_id": "T-99",
  "title": "Database Schema Audit",
  "access_list": [
    { "user_id": "alice_01", "role": "owner" },
    { "user_id": "bob_02", "role": "editor" },
    { "group_id": "engineering_team", "role": "viewer" }
  ]
}

Implementing the Filter

To retrieve only the tasks a user is authorized to see, you construct your queries to check if the user's ID exists within that array. For example, in a find operation, you would add a condition such as:

db.tasks.find({ "access_list.user_id": current_user_id })

This approach is highly efficient because you can build an index on the access_list.user_id field. By doing so, the database engine can instantly locate every document a specific user is permitted to access without scanning the entire collection.

When designing these lists, keep them manageable. If an access list grows to include thousands of members, performance can degrade during updates. In those cases, referencing a group_id as shown above is a much more scalable alternative to listing every individual user. By embedding these lists, you move security logic from the application layer down to the data layer, creating a robust, document-centric authorization model.

Key Takeaway: Embedding access lists directly within your documents enables fine-grained, indexable security that scales efficiently with your application's growth.