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

Share this content:

Post Comment