Course contentsShow
← System Design
Lesson 384 of 1,91911. NoSQL: Document StoresFree lesson

Querying Documents by Fields

Document stores support queries on any field within documents, including nested fields and array elements.

Querying Documents by Fields

What you'll learn: How document stores let you search and filter documents using any field inside them, no matter how nested or complex.

The Core Idea

Unlike key-value stores where you can only look up by the document ID (the key), document stores understand the structure inside your documents. You can query based on any field's value—whether it's a top-level property, a nested object, or even elements within arrays.

Think of it like searching your email. You don't just find messages by their ID number—you search by sender, subject, date, or keywords in the body. Document stores work the same way with your JSON documents.

How It Works

When you store a document like:

{
  "_id": "user123",
  "name": "Alice",
  "age": 28,
  "address": {
    "city": "Seattle",
    "zip": "98101"
  },
  "hobbies": ["hiking", "photography"]
}

You can query for:

  • All users where age > 25
  • Documents where address.city equals "Seattle" (nested field)
  • Users whose hobbies array contains "hiking"

The database parses the document structure and can filter on any path through the data. This flexibility means you don't need to know all your query patterns upfront—new queries can reference any field that exists in your documents.

Why This Matters

This querying capability bridges the gap between rigid relational schemas and simple key-value lookup. You get schema flexibility and powerful search, without needing to define indexes or relationships ahead of time (though indexes still help performance).

Key Takeaway: Document stores support rich queries on any field within documents—including nested objects and array elements—giving you flexible search without sacrificing schema-free design.