MongoDB Find Multiple Conditions


“While working in the MongoDB database, we tend to utilize the “find” function more often to display the data from the collections as a document. The find() function can be utilized in different ways possible. You can use it to display or restrict the display of the specific number of columns at your output by specifying the column names to 1 or 0, respectively. Along with that, we can also define the multiple conditions within the find() function of MongoDB to restrict the number of records for the collection. Within the SQL database, we try to specify the conditions within the WHERE clause. But, within MongoDB, we need to use other ways. Thus, we have decided to cover those methods in this guide. Let’s start with our new article now. Before diving into the implementation deep down, we have to log in from Ubuntu 20.04 system and open its terminal by the utilization of Ctrl+Alt+T. After opening the shell, it is high time to update our system before going further. These updates are necessary for the smooth implementation of our article. Thus, try the shown below instructions followed by the password of a current user to continue.”

Affirm this action by pressing “y” upon asked. Press Enter to continue. The processing will be displayed on your terminal screen. Within a few seconds, your system will get up to date with the latest versions.

After the update has been completed, we have to launch the MongoDB shell at our Ubuntu 20.04 shell. For this, use the “mongo” keyword command as we did below.

The terminal of MongoDB has been launched and is ready to use. Let’s display the list of available databases of MongoDB in which we want to work via the “show dbs” instruction at its shell area. It will show the total databases available. First, three of them are built-in and used to store configuration data. We will be using the user-defined “test” database in this tutorial. To use the “test” database, try out the “use” instruction with the database name “test.” Press the “Enter” key to execute this instruction.

To try out the multiple conditions within the find() function of MongoDB, we must have some collection in the “test” database and sufficient records within the collection. Right now, our database is empty. Thus, we need to create a new collection from scratch. We need to try out the “createCollection” function within the “db” instruction, followed by the name of a new collection to be created in the parenthesis. We have named the collection “Data.” The query was successful, and the collection was generated successfully as per the “ok: 1” status.

Now, we have a new and empty collection of “Data” within our database. We need to put some values as a MongoDB document in it. To insert the data within the MongoDB collection, we need to try out the db insertion with the insert() function preceded by the collection name. Thus, we have been using the same “db” instruction with our newly created collection name, i.e., Data and the insert() function taking values within it. We have been adding a different number of columns for each document record. The column names are: “_id,” “Name,” “City,” “Age,” “salary,” and “job.” Not every record contains all the columns, as we have mentioned. But, each record must contain the “_id,” “Name,” City,” and “Age” columns within it. A total of 15 records have been added with this insert() function command, as shown.

Before trying the conditions on the Data collection, we will be simply using the “find” function to fetch all its records at merienda on our screen. So, we have tried the find() function within the “db” command of our MongoDB. This command has been displaying all 15 records.

As we have mentioned before, we can restrict the number of columns to be displayed in our MongoDB shell by the use of options 1 and 0 with the column name. So, we will try that as well. We have been restricting the display of column “_id” at the MongoDB shell by setting the column value of “_id” to 0 within the find() function. It displayed all the columns except “_id.”

Let’s use the conditions in the find() function now. Let’s suppose you want to display the only records from the Data collection where the City is “Paris.” For this, you need to specify the “$or” variable, and within its [] brackets, specify the column name with the value “Paris” as we did in the command displayed below. A total of 2 document records have been found so far.

We can also specify the names of the columns to be displayed within the find() instruction as we have done it within the shown instruction so far. A total of 3 records have been found.

Let’s use more than 1 condition for the same column using the “$or” variable in the find() function. So, we have been searching for the records that contain the “job” column value as “Doctor,” “Engineer,” and “ShopKeeper.” We have also specified the columns to be shown. It displayed a total of 3 records.

Apart from column values, you can use the comparison operators as well. We have been using the less than “lt” operator in the find() function to display the records only where the ID is less than 6. It shows a total of 5 records.

Just like that, we have tried the greater than comparison operator for the “salary” column within the find function and got the 3 records in return.

Conclusion

This article is the best help to show you the use of the find() function with multiple conditions to display records of collection within MongoDB. We have tried to cover the most in our illustrations to make you understand how easy it is to do with the find() instruction. We have tried the column values and the comparison operators to limit the number of records or display the specific document records on the shell, i.e., less than, greater than operators.



Source link