Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Node.js MongoDB Filter Query

Node.js MongoDB Filter Query

Posted on October 31, 2025October 31, 2025 By Rishabh saini No Comments on Node.js MongoDB Filter Query

Node.js MongoDB Filter Query

When working with MongoDB in Node.js, one of the most common tasks is filtering data from a collection based on specific conditions. The find() method in MongoDB allows you to fetch data, and by using a query object, you can narrow down the results to match your desired criteria.

Data Science Tutorial:–Click Here

In this guide, we’ll explore how to filter MongoDB documents in Node.js — both by exact match and using regular expressions.

Filtering Data Using a Query Object

You can use the find() method with a query object to filter data according to a particular parameter. For example, let’s say we want to fetch the details of employees whose address is “Delhi”.

Example: Retrieve Employees from Delhi

Create a file named query1.js and add the following code:

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var query = { address: "Delhi" };
  db.collection("employees").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

Now, open your terminal and run the command below:

Introduction to Applied AI:–Click Here

node query1.js

This script connects to the MongoDatabase, retrieves all employee documents where the address field equals "Delhi", and displays them in the console.

Filtering Data Using Regular Expressions

In addition to exact matches, MongoDB allows you to use regular expressions to perform pattern-based searches on string fields. This approach is particularly useful when you want to find records where a field value starts, ends, or contains certain characters.

Download New Real Time Projects :–Click here

Example: Retrieve Employees Whose Address Starts with ‘L’

Create another file named query2.js and add this code:

var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  var query = { address: /^L/ };
  db.collection("employees").find(query).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});

Run the file using the command:

SQL Tutorial :–Click Here

node query2.js

This program fetches all employee records where the address field begins with the letter “L”. The regular expression /^L/ ensures that only those records are returned.

Deep Learning Tutorial:– Click Here
Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click Here


get data from mongodb node js using mongoose filter in node js mongodb mongodb find nodejs mongodb findone nodejs date filter in node js mongodb nodejs mongodb get collection find by id in mongodb node js mongodb find query example node js mongodb filter query example node js mongodb filter query w3schools mongodb query filter, mongodb query filter example, mongodb delete query node.js, node js mongodb query, mongodb filter, mongodb nodejs, how do i write a query in node js mongodb?, mongodb query, query filter, deleteone mongodb nodejs, mongodb query tips, nodejs graphql mongodb, mongodb projection filter, nodejs graphql and mongodb, node.js mongodb, mongodb query tutorial, can you query in mongodb?, how to write query in find method in mongodb, mongodb node.js, query projection in mongodb

    Post Views: 232
    Node.js Tutorial Tags:can you query in mongodb?, deleteone mongodb nodejs, how do i write a query in node js mongodb?, how to write query in find method in mongodb, mongodb delete query node.js, mongodb filter, mongodb node.js, mongodb nodejs, mongodb projection filter, mongodb query, mongodb query filter, mongodb query filter example, mongodb query tips, mongodb query tutorial, node js mongodb query, node.js mongodb, nodejs graphql and mongodb, nodejs graphql mongodb, query filter

    Post navigation

    Previous Post: Best Pharmacy Management System using MERN Stack
    Next Post: Best E-Commerce Products Clone – Full Stack MERN Application

    More Related Articles

    Nodejs REPL Nodejs REPL Node.js Tutorial
    Node.js vs Python Node.js vs Python – Python Project with Source Code Node.js Tutorial
    Install Nodejs on WindowsInstall Nodejs on Windows Install Nodejs on Windows Node.js Tutorial

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    You may also like

    1. Install Nodejs on Windows
    2. Node.js Command Line Options
    3. Node.js Assertion Testing
    4. Node.js MySQL Update Records
    5. Node.js MySQL Delete Records
    6. Node.js vs Python – Python Project with Source Code

    Most Viewed Posts

    1. Top Large Language Models in 2025
    2. Online Shopping System using PHP, MySQL with Free Source Code
    3. login form in php and mysql , Step-by-Step with Free Source Code
    4. Flipkart Clone using PHP And MYSQL Free Source Code
    5. News Portal Project in PHP and MySql Free Source Code
    6. User Login & Registration System Using PHP and MySQL Free Code
    7. Top 10 Final Year Project Ideas in Python
    8. Online Bike Rental Management System Using PHP and MySQL
    9. E learning Website in php with Free source code
    10. E-Commerce Website Project in Java Servlets (JSP)
    • AI
    • ASP.NET
    • Blockchain
    • ChatCPT
    • code Snippets
    • Collage Projects
    • Data Science Project
    • Data Science Tutorial
    • DBMS Tutorial
    • Deep Learning Tutorial
    • Final Year Projects
    • Free Projects
    • How to
    • html
    • Interview Question
    • Java Notes
    • Java Project
    • Java Script Notes
    • JAVASCRIPT
    • Javascript Project
    • JSP JAVA(J2EE)
    • Machine Learning Project
    • Machine Learning Tutorial
    • MySQL Tutorial
    • Node.js Tutorial
    • PHP Project
    • Portfolio
    • Python
    • Python Interview Question
    • Python Projects
    • PythonFreeProject
    • React Free Project
    • React Projects
    • Spring boot
    • SQL Tutorial
    • TOP 10
    • Uncategorized
    • Online Examination System in PHP with Source Code
    • AI Chatbot for College and Hospital
    • Job Portal Web Application in PHP MySQL
    • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
    • Online Job Portal System in JSP Servlet MySQL

    Most Viewed Posts

    • Top Large Language Models in 2025 (8,615)
    • Online Shopping System using PHP, MySQL with Free Source Code (5,218)
    • login form in php and mysql , Step-by-Step with Free Source Code (4,872)

    Copyright © 2026 UpdateGadh.

    Powered by PressBook Green WordPress theme