Azhagu-swe

  • Home
  • About
  • Experience
  • Skills
  • Projects
  • Contact

  • Tutorial
  • Blog
Beginner
20 min read

Flexible Data Models: An Introduction to NoSQL

Published on July 16, 2025

What is NoSQL?

NoSQL, which stands for "Not Only SQL," is a term for a broad category of database management systems that differ from the traditional relational (SQL) database model. While SQL databases use a structured schema with tables, rows, and columns, NoSQL databases come in a variety of types, allowing for more flexible data models.

The most common type of NoSQL database is a document-oriented database, like MongoDB. Instead of rows in a table, data is stored in flexible, JSON-like documents.

SQL vs. NoSQL: Key Differences

  • Schema: SQL databases have a rigid, predefined schema. NoSQL databases often have a dynamic schema, where documents in the same collection can have different structures.
  • Scalability: NoSQL databases are typically designed to scale out horizontally (by adding more servers), making them well-suited for large, distributed data sets.
  • Data Model: SQL uses tables. NoSQL uses various models, including documents, key-value pairs, wide-column stores, and graph databases.

1. Document Databases: A Quick Look

In this tutorial, we'll use syntax from MongoDB, one of the most popular document databases.

In MongoDB, data is stored in collections (similar to SQL tables). Each entry in a collection is a document (similar to an SQL row). A document is a data structure composed of field and value pairs, similar to a JSON object.

Here's an example of a Users collection:

json
[
  {
    "_id": "ObjectId('...')",
    "name": "Azhagu",
    "email": "azhagu@example.com",
    "age": 30,
    "hobbies": ["coding", "reading"]
  },
  {
    "_id": "ObjectId('...')",
    "name": "Jane Doe",
    "email": "jane@example.com",
    "age": 25,
    "location": "New York"
  }
]

Notice how flexible this is! The first user has an array of hobbies, while the second has a location. This would be difficult to model in a rigid SQL table.


2. find(): Reading Data

To retrieve data in MongoDB, you use the find() method on a collection.

To find all users in our Users collection, you would run:

javascript
db.Users.find({});
  • db.Users: This specifies the database and the collection.
  • find({}): The curly braces {} represent an empty query document, which matches all documents in the collection.

To filter the results, you pass a query document with the conditions. Let's find the user whose name is "Jane Doe":

javascript
db.Users.find({ name: "Jane Doe" });

3. insertOne(): Creating Data

To add a new document to a collection, you use the insertOne() method.

Let's add a new user:

javascript
db.Users.insertOne({
    name: "Peter Jones",
    email: "peter@example.com",
    age: 42,
    tags: ["developer", "full-stack"]
});

You simply pass a document (a JSON-like object) containing the data for the new user. The _id field is automatically generated by MongoDB.


4. updateOne(): Modifying Data

To change an existing document, you use the updateOne() method. This method takes two arguments:

  1. A query document to select the document you want to update.
  2. An update document that specifies the changes.

Let's update the email address for the user named "Azhagu".

javascript
db.Users.updateOne(
    { name: "Azhagu" },
    { $set: { email: "new.email@example.com" } }
);
  • The first document { name: "Azhagu" } is the filter.
  • The second document uses the $set operator to specify which field to update and its new value.

5. deleteOne(): Removing Data

To remove a document from a collection, you use the deleteOne() method. You pass it a query document to specify which document to delete.

Let's remove the user named "Peter Jones":

javascript
db.Users.deleteOne({ name: "Peter Jones" });

This finds the first document that matches the filter { name: "Peter Jones" } and removes it from the collection.


Conclusion

You've just been introduced to the world of NoSQL databases! You've learned the fundamental differences between SQL and NoSQL, seen how data is structured in a document database like MongoDB, and explored the basic CRUD operations: find, insertOne, updateOne, and deleteOne.

NoSQL databases offer incredible flexibility and scalability, making them a powerful choice for many modern applications. As you continue your development journey, understanding when to use a relational database and when to use a NoSQL database will be a valuable skill. Happy coding!

Table of Contents
  • 1. What is NoSQL?
  • 1.1 SQL vs. NoSQL: Key Differences
  • 2. Document Databases: A Quick Look
  • 3. `find()`: Reading Data
  • 4. `insertOne()`: Creating Data
  • 5. `updateOne()`: Modifying Data
  • 6. `deleteOne()`: Removing Data
  • 7. Conclusion
Back to All Tutorials
Azhagu-swe

A Full Stack Developer passionate about creating modern, scalable web applications.

Quick Links
Connect With Me

© 2025 Azhagu-swe. All rights reserved.

Crafted with ❤️ By Azhagu-swe