How to write a function to get rows from database by multiple columns conditionally ?
Last Updated :
17 Aug, 2021
MongoDB, the most popular NoSQL database, we can get rows from the database by multiple columns conditionally from MongoDB Collection using the MongoDB collection.find() function with the help of $or or $and operators. The mongodb module is used for connecting the MongoDB database as well as used for manipulating the collections and databases in MongoDB.
collection.find() is the method of the MongoDB module in the node.js that is used to select the rows from the collections of a particular database present in the database of the MongoDB.
Syntax:
db.collection.find(query,projection)
Parameters: This method takes two parameters that are not required by default:
- query: Selection query that is used to fetch the data from the database with the help of selection operators.
- projection: Projection is used to specify which field of the collection returns or not.
Return Type: The return type of this function is a JSON object.
$or or $and operators are used to apply multiple condition on the collection.
Installing Module: You can install the mongodb module using the following command:
node install mongodb
Project Structure: The project structure will look like the following.

Running the server on Local IP: In the following command, data is the folder name.
mongod --dbpath=data --bind_ip 127.0.0.1

MongoDB Database: Our database name and collection is shown below with some dummy data.
Database:GFG
Collection:gfg2
Filename: index.js
JavaScript
// Requiring module
const MongoClient = require("mongodb");
// Connection URL
const url='mongodb://localhost:27017/';
// Our database name
const databasename="GFG";
MongoClient.connect(url).then((client) => {
const connect = client.db(databasename);
// Connecting to collection
const collection = connect.collection("gfg2");
// Function call with $or operator
collection.find({$or:[{"name":"GFG"},{"marks":"10"}]}).toArray().then((ans)=>{
console.log(ans);
});
}).catch((err) => {
// Printing the error message
console.log(err.Message);
})
Run index.js file using below command:
node index.js
Output:
Similar Reads
How to Efficiently Convert Rows to Columns in PL/SQL? In Oracle PL/SQL, converting rows into columns is a common operation, especially useful for reporting, data analysis, and reformatting data for easy visualization. PL/SQL, or Procedural Language/Structured Query Language, is a powerful procedural extension to SQL, created by Oracle, that integrates
5 min read
How to Add a Conditional Column in Power Query in Excel? We may use Power Query to generate new columns whose values depend on one or more criteria that have been applied to existing columns in your database. The condition imposed on an existing column in the data model serves as the foundation for Power BI's conditional column. On the Add column tab, in
2 min read
How to Show all Columns in the SQLite Database using Python ? In this article, we will discuss how we can show all columns of a table in the SQLite database from Python using the sqlite3 module. Approach:Connect to a database using the connect() method.Create a cursor object and use that cursor object created to execute queries in order to create a table and
3 min read
How to Filter Data Using Conditions Joined by AND Operator In the field of data analysis and database processing, efficient filtering is critical to obtain significant information. Filtering is based on the selection of data where the data criteria are applied. One commonly employed method is using the AND operator to join multiple conditions, allowing for
4 min read
Filter Rows Based on Conditions in a DataFrame in R In this article, we will explore various methods to filter rows based on Conditions in a data frame by using the R Programming Language. How to filter rows based on Conditions in a data frame R language offers various methods to filter rows based on Conditions in a data frame. By using these methods
3 min read
Selecting Multiple Columns Based On Condition in SQL SQL (Structured Query Language) is used to manage and query databases. One common requirement when querying data is selecting multiple columns based on specific conditions. Understanding how to use SQL for this purpose can enhance your ability to retrieve relevant data efficiently. In this article,
4 min read