Example 1 — Sample Data
Imagine two users. Asha has every required permission. Ben is missing delete. We save both in a collection named users.
db.users.insertMany([
{
_id: 1,
name: "Asha",
required: ["read", "write"],
tags: ["read", "write", "admin"]
},
{
_id: 2,
name: "Ben",
required: ["read", "delete"],
tags: ["read", "write"]
}
]); How the Data Is Stored in MongoDB
After the insert, MongoDB keeps two documents in users. Run find() to see them:
db.users.find() Read those documents like this:
| Field in MongoDB | Example (Asha) | Plain English |
|---|---|---|
_id | 1 | Document id |
name | "Asha" | User name |
required | ["read", "write"] | Permissions that must be present (array A) |
tags | ["read", "write", "admin"] | Permissions the user has (array B) |
Asha:
required ⊆ tags ?
{read, write} ⊆ {read, write, admin} → true
Ben:
required ⊆ tags ?
{read, delete} ⊆ {read, write} → false
(missing "delete") How It Works
- Insert once.
insertManyadds two user documents tousers. - Two arrays per user.
requiredis the candidate subset;tagsis the set to search. - find() shows both rows. What you inserted is what you get back—ready for the subset check next.

