EZ

Eduzan

Learning Hub

Back to NOSQL

Views in NoSQL Databases

Published 2025-12-09

NoSQL

Definition: In NoSQL databases, views are not as universally defined or as prevalent as in SQL databases because NoSQL does not inherently support traditional views due to its non-relational nature. However, some NoSQL databases like CouchDB and MongoDB have mechanisms that serve similar purposes as SQL views, offering ways to manage and query derived sets of data.

1. Creating Views in CouchDB

Definition: In CouchDB, views are defined using JavaScript functions and are stored as part of design documents. They provide a way to create queryable indexes based on functions that map, filter, and reduce data.

  • Example:
{
  "_id": "_design/example",
  "views": {
    "by_name": {
      "map": "function(doc) { if (doc.Name && doc.Type == 'Customer') emit(doc.Name, null); }"
    }
  }
}

Explanation: This creates a view named by_name in a design document in CouchDB. It maps documents of type ‘Customer’ by their ‘Name’. This view acts like a virtual table that can be queried to retrieve customers by name.

1.1 Updating Views

Definition: Since views in NoSQL databases like CouchDB are inherently read-only and the result of a map-reduce operation, they cannot be updated directly like SQL views. However, the underlying data can be updated, which will be reflected in the view upon re-querying.

  • Example:
db.customers.insert({ "_id": "doc1", "Name": "John Doe", "Type": "Customer"});
// Update document
db.customers.update({"_id": "doc1"}, {$set: {"Name": "Jane Doe"}});

Explanation: After updating the document, re-querying the by_name view will reflect the changes because the view will recompute based on the updated data.

1.2 Dropping Views

Definition: Dropping a view in CouchDB involves removing or modifying the design document that contains the view definition.

  • Example:
db.design_documents.remove({_id: '_design/example'});

Explanation: This deletes the design document named ‘example’, effectively removing all views defined within it from the database.

1.3 Materialized Views in MongoDB

Definition: MongoDB supports a similar concept through the use of materialized views, which are essentially pre-computed results stored as collections. These are created via the aggregation pipeline and need to be manually refreshed to stay current.

  • Example:
db.sales.aggregate([
  {$match: {}},
  {$group: {_id: "$productId", totalQuantity: {$sum: "$quantity"}}},
  {$out: "sales_summary"}
]);

Explanation: This aggregation pipeline groups sales by product ID and calculates the total quantity sold, outputting the results to a new collection called sales_summary. This collection acts as a materialized view.

CONCLUSION

While traditional SQL views provide a dynamic window into stored data, NoSQL views (or their equivalents) often involve statically stored results of queries or functions that need manual updating. They serve to optimize read operations but lack the dynamic updating feature of SQL views. In NoSQL systems, understanding how to properly leverage these tools can greatly enhance data retrieval performance and facilitate complex data aggregation.