Building Custom Query Builder: Simplify Database Queries Like a Pro! 🚀

Govindram Solanki
3 min readMar 17, 2023

--

Effortlessly Build Complex Queries with CustomQuerybuilder: A Complete Guide with Code Examples

Image generated by NightCafe. AI powered image generator

👋 Hey there!
Are you looking for a way to query your MongoDB data dynamically? Look no further than this code snippet!

In this article, I’ll be explaining how to use this code to create dynamic queries in MongoDB and help you understand how to modify it to fit your specific needs.Here is the link to my project..

Project Repository :

#1 Understanding the Code 🔍

Before we dive into how to use this code, let’s take a quick look at how it works. At a high level, the code is designed to generate MongoDB queries based on a set of predefined rules. These rules are defined by a set of comparison operators and data types (such as)

Comparison Operators :-
-is
-is not
-starts with
-ends with
-greater than
-less than
-has
-doesn’t have
-on
-before
-after
-between
-more than

Data types :-
-number
-date
-string

The code then uses these rules to create a set of MongoDB operators that can be used to query the database.

Here’s a brief overview of the different parts of the code:

  • queryGenerator: This function takes a set of query predicates and uses them to build a MongoDB query.
  • baseOperatorBuilder: This function takes a single predicate and builds a MongoDB operator based on its properties.
  • OPERATORS and OPERATOR_FUNCTIONS: These two objects define the different comparison operators and their corresponding MongoDB operators.

#2 Using the Code 🔧

Now that we understand how the code works, let’s talk about how to use it. The first thing you’ll need to do is create a set of query predicates. Each predicate should include the following properties:

  • type: The data type of the property being queried.
  • property: The name of the property being queried.
  • comparison: The comparison operator being used.
  • value: The value being compared against.

Here’s an example of a set of query predicates:

{
predicate: {
type: "and",
predicates: [
{
type: "string",
property: "name",
comparison: "equals",
value: "John Doe",
},
{
type: "number",
property: "age",
comparison: "greater than",
value: 18,
},
{
type: "date",
property: "created_at",
comparison: "between",
value: [new Date("2022-01-01"), new Date("2022-02-01")],
},
],
},
};

you can query at grain level using the nested predicates :

{
predicate: {
type: "or",
predicates: [
{
property: "name",
comparison: "has",
type: "string",
value: "am",
},
{
type: "and",
predicates: [
{
property: "email",
comparison: "is",
type: "string",
value: "Gram@abc3.com",
},
],
},
],
},
}

Once you have your predicates, you can use the queryGenerator function to generate a MongoDB query:

const query = queryGenerator(predicates);

This query can then be passed to a MongoDB find operation:

const results = await User.find(query);

And that’s it!😎 You now have a dynamic way to query your MongoDB data.

#3 Modifying the Code 👨‍💻

Of course, this code may not be exactly what you need for your specific use case. Fortunately, it’s easy to modify the code to fit your needs. Here are a few examples:

  • Adding new comparison operators: If you need to add a new comparison operator, simply add it to the OPERATORS object and define its corresponding MongoDB operator in the OPERATOR_FUNCTIONS object.
  • Changing the data types: If you need to add a new data type, simply add it to the OPERATORS object and define how it should be converted to a MongoDB operator in the OPERATOR_FUNCTIONS object.
  • Changing the schema: If you need to change the schema for your collection, simply modify the userSchema object to match your new schema.

# Conclusion 🎉

In this article, we’ve covered how to use this code to create dynamic MongoDB queries with the help of recursive function 🎯.

Note of Gratitude

If you have any further question, or you know any other extension, feel free to comment!

See you next time. Bye!

--

--