Skip to content
FullStackDost Logo
  • All Courses
  • Blogs
  • Login
  • Register
  • All Courses
  • Blogs
  • Login
  • Register
  • Home
  • All Courses
  • Web development
  • MongoDB – (No-SQL)

MongoDB – (No-SQL)

Curriculum

  • 10 Sections
  • 31 Lessons
  • 10 Weeks
Expand all sectionsCollapse all sections
  • Introduction to MongoDB
    MongoDB is a NoSQL database that is designed for handling large volumes of unstructured or semi-structured data. Unlike traditional relational databases (RDBMS) that use tables and rows to organize data, MongoDB stores data in a flexible document-oriented format using JSON-like documents (BSON - Binary JSON). This makes it highly scalable, flexible, and performant for applications that need to handle varying types of data with complex structures.
    5
    • 1.1
      What is MongoDB?
    • 1.2
      Why MongoDB?
    • 1.3
      When to use MongoDB?
    • 1.4
      Key Features of MongoDB
    • 1.5
      Installing MongoDB
  • MongoDB Basic Operations
    MongoDB provides a rich set of basic operations for interacting with the database, including creating, reading, updating, and deleting data (often abbreviated as CRUD operations). Below are the basic operations that you can perform with MongoDB.
    2
    • 2.0
      Database and Collection Basics
    • 2.1
      CRUD Operations
  • Advanced Querying Techniques
    MongoDB offers a rich set of querying capabilities, and as you work with larger datasets and more complex application requirements, you’ll often need to use advanced querying techniques. These techniques help you optimize performance, execute sophisticated queries, and leverage MongoDB’s powerful indexing and aggregation features.
    4
    • 3.1
      Query Filters and Operators
    • 3.2
      Advanced Querying
    • 3.3
      Sorting and Limiting Results
    • 3.4
      Aggregation Framework
  • Data Modeling and Schema Design
    Data modeling and schema design are critical when using MongoDB (or any NoSQL database) to ensure efficient data storage, fast queries, and scalability. Unlike relational databases, MongoDB is schema-less, which means you are not required to define a fixed schema upfront. However, making the right design decisions from the beginning is essential for maintaining performance and avoid complications as your data grows.
    4
    • 4.1
      Data Modeling
    • 4.2
      Document Structure
    • 4.3
      Schema Design Patterns
    • 4.4
      MongoDB and Relationships
  • Indexing and Performance Optimization
    In MongoDB, indexing is a critical part of performance optimization. Without proper indexes, MongoDB has to scan every document in a collection to satisfy queries, which can be very inefficient for large datasets. Indexes are used to quickly locate data without scanning every document, making reads faster and more efficient.
    3
    • 5.0
      Creating Indexes
    • 5.1
      Using Text Search
    • 5.2
      Performance Optimization
  • Integrating MongoDB with a Web Application (Node.js)
    Integrating MongoDB with a web application built using Node.js is a common and powerful combination for building scalable and efficient web apps. MongoDB’s flexibility with JSON-like data and Node.js's asynchronous event-driven architecture work well together. In this guide, I'll walk you through the steps for integrating MongoDB with a Node.js web application, covering the essentials of setting up the connection, performing CRUD operations, and using popular libraries.
    3
    • 6.0
      Setting Up MongoDB with Node.js
    • 6.1
      CRUD Operations with Mongoose
    • 6.2
      Error Handling and Validation
  • Security in MongoDB
    Security is an essential aspect when working with MongoDB, especially when handling sensitive data in production environments. MongoDB provides a variety of security features to help protect your data against unauthorized access, injection attacks, and other vulnerabilities. Here’s a guide on securing MongoDB and your Node.js application when interacting with MongoDB.
    2
    • 7.0
      Authentication and Authorization
    • 7.1
      Data Encryption
  • Working with MongoDB in Production
    3
    • 8.0
      MongoDB Backup and Restore
    • 8.1
      MongoDB Scaling and Sharding
    • 8.2
      MongoDB Replication
  • Deploying and Monitoring MongoDB
    Working with MongoDB in a production environment requires careful planning, attention to detail, and best practices to ensure optimal performance, security, reliability, and scalability.
    3
    • 9.0
      Deploying MongoDB to Production
    • 9.1
      Monitoring and Management
    • 9.2
      Summary for MongoDB deployment on Production
  • Building a Web App with MongoDB (Final Project)
    Demo Project (OneStopShop)
    2
    • 10.0
      Building the Application
    • 10.1
      Final Project Features

Using Text Search

MongoDB provides text search capabilities to perform full-text search on string fields within documents. With the help of text indexes, MongoDB can efficiently find documents containing specific words or phrases in text fields. Text search is ideal for situations where you need to perform searches on large amounts of unstructured text, such as descriptions, comments, articles, or other documents containing freeform text.

1. Creating a Text Index

A text index is an index on string fields that allows for full-text search functionality. You can create a text index on one or more fields, and MongoDB will automatically tokenize the text and provide features like stemming, case insensitivity, and support for wildcard searches.

Syntax:

To create a text index on one or more fields, you use the "text" option when calling createIndex().

javascript

Copy code

db.collection.createIndex({ fieldName: "text" })

Example:

Let’s say you have a posts collection, and you want to perform text searches on the title and content fields. You can create a text index on both fields:

javascript

Copy code

db.posts.createIndex({ title: "text", content: "text" })

This will allow MongoDB to perform full-text searches on both title and content fields.

2. Basic Text Search

Once a text index is created, you can use the $text operator in the find() method to search for specific words or phrases in text-indexed fields.

Syntax:

javascript

Copy code

db.collection.find({ $text: { $search: "searchTerm" } })

Example:

Let’s assume you want to search for documents containing the word “MongoDB” in the title or content fields of the posts collection:

javascript

Copy code

db.posts.find({ $text: { $search: "MongoDB" } })

This query will return all posts where either the title or content contains the word “MongoDB”.

3. Text Search with Phrases

You can search for exact phrases by enclosing the phrase in double quotes. MongoDB will search for documents containing that exact phrase.

Syntax:

javascript

Copy code

db.collection.find({ $text: { $search: "\"exact phrase\"" } })

Example:

Search for posts that contain the exact phrase “MongoDB tutorial”:

javascript

Copy code

db.posts.find({ $text: { $search: "\"MongoDB tutorial\"" } })

This will only return documents that contain the exact phrase “MongoDB tutorial” in either the title or content fields.

4. Text Search with AND and OR Operators

MongoDB supports basic boolean logic in text searches, including the AND and OR operators.

  • AND: The space between words acts as an implicit AND. For example, searching for "MongoDB tutorial" will return documents that contain both “MongoDB” and “tutorial”.
  • OR: You can use the "$search" operator to include multiple words or phrases and find documents that match any of them. Use a space between terms to indicate OR logic.

Example:

To search for documents that contain both “MongoDB” and “tutorial”:

javascript

Copy code

db.posts.find({ $text: { $search: "MongoDB tutorial" } })

This will return all documents containing both “MongoDB” and “tutorial”.

Example of OR (Multiple Words Search):

You can also search for documents that contain either of two words:

javascript

Copy code

db.posts.find({ $text: { $search: "MongoDB OR tutorial" } })

This will return documents that contain either “MongoDB” or “tutorial”, or both.

5. Text Search with Exclusion (-)

MongoDB allows excluding words from the search results using a minus sign (-). If you place a minus sign before a word, MongoDB will exclude documents that contain that word.

Syntax:

javascript

Copy code

db.collection.find({ $text: { $search: "term1 -term2" } })

Example:

To search for documents that contain the word “MongoDB” but not “tutorial”:

javascript

Copy code

db.posts.find({ $text: { $search: "MongoDB -tutorial" } })

This will return all documents that contain the word “MongoDB” but exclude those that also contain the word “tutorial”.

6. Text Search with Score

MongoDB assigns a relevance score to each document based on how well it matches the search terms. You can retrieve these scores to rank search results.

To include the relevance score in the query results, you can use the score in the projection.

Syntax:

javascript

Copy code

db.collection.find( { $text: { $search: "searchTerm" } }, { score: { $meta: "textScore" } } )

Example:

To search for “MongoDB” and include the relevance score in the result:

javascript

Copy code

db.posts.find( { $text: { $search: "MongoDB" } }, { score: { $meta: "textScore" } } ).sort({ score: { $meta: "textScore" } })

This query returns documents containing “MongoDB” and sorts them by relevance, with documents that match the search term more closely receiving a higher score.

7. Text Search with Language-Specific Features

MongoDB’s text search is language-aware, meaning that it uses language-specific rules for stemming (reducing words to their base form) and stop words (common words like “the”, “and”, etc., that are ignored during the search).

You can specify the language for a text index, or let MongoDB default to a language (usually english).

Syntax for Specifying Language:

javascript

Copy code

db.collection.createIndex({ fieldName: "text" }, { default_language: "english" })

Example:

Creating a text index for a collection with a specific language:

javascript

Copy code

db.posts.createIndex({ title: "text", content: "text" }, { default_language: "english" })

This index will apply English-specific rules for stemming and stop word removal.

8. Text Search Limitations

  • Language Limitations: By default, MongoDB uses stemming and stop words specific to a default language (e.g., English). If you’re working with other languages, you may need to adjust the language settings when creating the text index.
  • Field Type Limitations: Only string fields can be indexed as text fields. Text search cannot be performed on numeric, date, or binary fields.
  • Search Performance: While text search is fast for smaller datasets, it can become slower for very large collections. Proper indexing and using projections to limit the number of fields returned can help.
  • No Wildcards: MongoDB’s text search does not support wildcard characters (e.g., *, ?) or regular expressions for matching text in the way you might find in some search engines.

9. Text Search with Aggregation

You can also use text search in MongoDB’s aggregation framework to perform more advanced queries, like filtering, sorting, and transforming data based on text search results.

Example:

To perform a text search within an aggregation pipeline and include the score, use the $match and $project stages:

javascript

Copy code

db.posts.aggregate([ { $match: { $text: { $search: "MongoDB" } } }, { $project: { title: 1, score: { $meta: "textScore" } } }, { $sort: { score: { $meta: "textScore" } } } ])

This aggregation pipeline:

  1. Filters documents to include only those that match the text search for “MongoDB”.
  2. Projects the title field and includes the score.
  3. Sorts the results by the relevance score in descending order.

10. Using Atlas Full-Text Search

For more advanced full-text search features, such as phrase matching, wildcard queries, and more complex analyzers (including support for multiple languages and custom analyzers), MongoDB Atlas offers a powerful Atlas Full-Text Search feature built on Lucene.

  • Atlas Full-Text Search supports more advanced search features than the standard MongoDB text indexes, such as:
    • Phrase queries
    • Fuzzy matching
    • Autocompletion
    • Search suggestions
    • Complex filters

You can create and manage Atlas Search Indexes directly from the MongoDB Atlas UI.

Creating Indexes
Prev
Performance Optimization
Next

Copyright © 2025 FullStackDost. All Rights Reserved.

Theme by ILOVEWP