Node.js – MongoDB Query


Node.js – MongoDB Query



”;


The find() and findOne() methods defined in mongodb driver module for Node.js returns all documents or the first document from the specified collection that satisfy the query argument. You can use logical operators to construct a filter in the query object as follows −

MongoDB Operators

MongoDB doesn”t use traditional logical operator symbols. Instead, it has its own operators as listed below −











Sr.No MongoDB Operator & Description
1

$eq

equal to (==)

2

$gt

greater than (>)

3

$gte

greater than or equal to (>=)

4

$in

if equal to any value in array

5

$lt

less than (<)

6

$lte

less than or equal to (<=)

7

$ne

not equal to (!=)

8

$nin

if not equal to any value in array

The operators are used in find() method to apply filter. Following statement returns products with price>10000

Example


const {MongoClient} = require(''mongodb'');

async function main(){

   const uri = "mongodb://localhost:27017/";
   const client = new MongoClient(uri);

   try {
      // Connect to the MongoDB cluster
      await client.connect();

      // Make the appropriate DB calls

      // Create a single new listing
      await fetchdocs(client, "mydb", "products");
       
   } finally {
      // Close the connection to the MongoDB cluster
      await client.close();
   }
}

main().catch(console.error);


async function fetchdocs(client, dbname, colname){
   const result = await client.db(dbname).collection(colname).find({"price":{$gt:10000}}).toArray();
   console.log(JSON.stringify(result));
}

Output


[{"_id":"6580964f20f979d2e9a72ae7","ProductID":1,"Name":"Laptop","price":25000},{"_id":"6580964f20f979d2e9a72ae8","ProductID":2,"Name":"TV","price":40000}]

The $and as well as $or operators are available for compound logical expressions. Their usage is as follows −


db.collection.find($and:[{"key1":"value1"}, {"key2":"value2"}])

Use following command to fetch products with price between 1000 and 10000.


async function fetchdocs(client, dbname, colname){
   const result = await client.db(dbname).collection(colname).find({$and:[{"price":{$gt:1000}}, {"price":{$lt:10000}}]}).toArray();
   console.log(JSON.stringify(result));
}

Output


[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]

Regex

You can also create the filter by forming regex expressions. The $regex variable is used as the key in the query JSON representation. The following code returns all the products whose name starts with P.

Example


async function fetchdocs(client, dbname, colname){
   const result = await client.db(dbname).collection(colname).find({Name:{$regex:"^P"}}).toArray();
   console.log(JSON.stringify(result));
}

Output


[{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]

In the following example, the resultset contains the documents with name starting with Ro.


async function fetchdocs(client, dbname, colname){
   const result = await client.db(dbname).collection(colname).find({Name: /Ro/}).toArray();
   console.log(JSON.stringify(result));
}

Output


[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000}]

To obtain the products with names ending with er, use the $ symbol at the end.


async function fetchdocs(client, dbname, colname){
   const result = await client.db(dbname).collection(colname).find({Name: /er$/}).toArray();
   console.log(JSON.stringify(result));
}

Output


[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]

Advertisements

”;

Leave a Reply

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