Make money with Oziconnect referral program

Searching databases and extracting relevant data can be very tedious if you don’t have the appropriate tools or don’t know how to use them.

MongoDB is a non-relational, non-SQL database that is different from relational SQL-based databases such as PostgresSQL and MySQL.

These SQL-based databases use traditional rows and columns to represent data, whereas MongoDB uses collections. Because of this key difference, it is important to understand certain specialized terminology specific to MongoDB.

The inspiration for this tutorial came from working on a personal project. I needed to implement a feature that required complex MongoDB query operations. Scrolling through the MongoDB documentation on this issue made it even more confusing and incomprehensible.

With the help of several websites and books, I was finally able to do it. Therefore, the purpose of this tutorial is to simplify complex search operations in MongoDB for new MongoDB users.

I hope this article answers your burning questions and helps you navigate MongoDB query operations.

Before we begin, here are some important prerequisites for this tutorial.

  • Basic knowledge of Node.js
  • Basic knowledge of MongoDB
  • Knowledge of MongoDB commands
  • postman knowledge

What is a MongoDB query?

A query is a command used to retrieve data from a MongoDB database. They function similarly to the SQL query system, but syntactically they behave differently.

A traditional SQL query looks like this:

“SELECT * FROM db.Users”(SQL) vs  “db.collection.find(MONGO DB)”

This command allows you to retrieve all data stored in the user database.

You can use many query operators on MongoDB database collections to retrieve related information. However, in this tutorial we will discuss some of the related ones in more detail.

MongoDB query example

Next, we will discuss some of the query operators available in MongoDB. You can run the code sample provided below in the MongoDB command line interface to see it in action.

Find(): This operator returns all documents in the collection. You can test this by running:

Db.collection.find()

In this case please replace collection Enter the actual name of the collection you want to search.

findOne(): This query operator returns the first document in the collection that matches the filter attached to the operator.

Db.collection.findOne()

Aggregate(): This operator matches results from different documents in the specified collection. It can be combined with other query operators to organize results and efficiently group different data.

An example of how this operator can be used alongside the limit and sort query operators.

limit(): This operator limits the total expected search results to the specified number.

db.collection.aggregate([ $limit: 6 ]);

The above code limits the sum of aggregated data to 6.

Sort(): This operator sorts the results of a search query in ascending or descending order.

db.collection.aggregate([
   $sort:  fieldName: 1   // Replace 'fieldName' with the actual field name and use 1 for ascending or -1 for descending order
]);

You can test these query operators in standard web applications. There are many programming tools available for developing applications, but this tutorial uses Node.js. This is because Node.js is less complex to use and is easily compatible with MongoDB database applications.

How to implement search queries in MongoDB using Node.js

Node.js is a JavaScript-based backend language and will be the primary language you will use when using MongoDB in this tutorial.

Here you will write code to search for documents using the Node.js Express backend framework.

Mongoose acts as a connection between MongoDB and Node. So, before we get into details, what is a mongoose?

What is a mongoose?

Mongoose is a popular object-relational mapping (ORM) tool that helps establish efficient connections between databases (MongoDB) and object-oriented programming languages ​​(Node.js/JavaScript).

It provides extensive functionality such as data modeling, schema development, model authentication, and data management that simplifies web API and database interaction.

You can also use it to interact with other databases such as Redis, MySQL, and Postgres.

Now let’s set up Mongoose.

Npm install mongoose

Use the following code to connect to MongoDB in your Node.js application.

const mongoose = require("mongoose");

mongoose.connect('mongodb://localhost/location', 
  useNewUrlParser: true,
  useUnifiedTopology: true,
  autoIndex: true
)
  .then(() => 
    console.log("Connected to MongoDB");
  )
  .catch((err) => 
    console.error(err);
  );


This code initiates a connection with the MongoDB database and maintains the connection to enable data exchange with the backend application.

How to search documents in MongoDB

In this section, you’ll apply some of the MongoDB operations you learned in the previous section to further improve your search queries. Retrieve books into the database based on . findOne These are the parameters that were briefly explained earlier.

First, let’s use it Find() Operators like this:

router.get("/", async (req, res) => 
  try 
    const books = await Book.find();
    res.status(200).json(books);
   catch (err) 
    res.status(500).json(err);
  
);


The above code will request all books in the database. If successful, a status code of 200 is returned in JSON format along with all the books in the collection. If unsuccessful, an error code is returned.

How can it be applied? Limit() Operators like this:

router.get("/", async (req, res) => 
  try 
    let limitedBooks = await Book.find().limit(6);
    res.status(200).json(limitedBooks);
   catch (err) 
    res.status(500).json(err);
  
);

This code is very similar to the code above. However, an additional restriction operator is attached, which limits the expected response to the first six books of his from the database.

And finally, FindOne() operator:

router.get("/", async (req, res) => 
  try 
    let myBook = await Book.findOne( Author: "Man" );
    res.status(200).json(myBook);
   catch (err) 
    res.status(500).json(err);
  
);

In the code above, we tried to find the first book written by a person called “Man”. If the document is successfully found, a success code of 200 and the JSON format of that book collection in the database are returned. Otherwise, an error code is returned.

How to search between text in MongoDB

Searching between texts requires a more complex approach to searching MongoDB databases.

This involves searching for texts and phrases across databases and displaying information for objects containing these searched texts.

This is often used in complex search operations to include only information grouped based on price, author, address, age, or other relevant common variables.

Now let’s implement a special MongoDB search query operator. Use this operator to search between text and return results as needed.

This code is displayed below.

let myBook = await Book.find(
  "$or": [
     Author:  $regex: req.params.key  ,
     Title:  $regex: req.params.key  ,
  ]
);


The above code uses regular expression format to search for a term in the database and return it. Regular expressions work on the principle of matching sets of strings with similar patterns. This provides faster responses to search queries and returns only documents similar to your search.

conclusion

This concludes the tutorial. You now have a basic understanding of MongoDB search queries and how to utilize different search operators to get the best possible results from your database.

Please feel free to post your comments and questions.Also, check out my other articles here. Until next time, keep coding.

Make money with Oziconnect referral program
Make money with Oziconnect referral program
Make money with Oziconnect referral program
84512

About Us

We are a leading IT agency in Lagos, Nigeria, providing IT consulting and custom software development services. We offer a wide range of IT solutions across software development, web and mobile application development, blockchain development services, digital marketing, and branding.

Contact Us

25B Lagos-Abekouta Expressway Lagos

info@ozitechgroup.com

Phone: (234) 907 155 5545

@2023 OzitechGroup – All Right Reserved.