Internet Tutorials How to Make Queries in MongoDB Next in this new post we will show you a small guide ...

Internet Tutorials How to Make Queries in MongoDB Next in this new post we will show you a small guide …

Next in this new post we will show you one small guide for you to learn to perform queries in MongoDB. In this way you will be able to acquire the basic knowledge to know if non-relational databases adapt to our scheme. Otherwise it will be necessary to find another more suitable solution.

Regarding Mongo DB, we can define it as a program in charge of handling non-relational databases. That is, it behaves differently from MySQL or PostgreSQL. This makes running the data structure unprecedented. But without a doubt, initially the queries are greatly affected due to that MongoDB behavior.

Mongo DB Installation Process

Before you can proceed to query in MongoDB we must first complete its installation process. This won’t take long, so pay attention to the following guidelines.

  • The first thing you should do is go to the official MongoDB page to download the appropriate version for our operating system. Remember that it’s totally free and has support for Linux, Mac OS X, Windows and Solaris.
  • Later when the download is finished, you have to start the mongo daemon. If you are using Windows, you have to start exe. But for that works well you have to create a directory on a route that does not need any administrator permission. Then when you start mongod.exe you must tell it which route to take.
  • Now you must enter the following command Of start:
$rutaMongodb/ mongod.ex - - dbpath “Unidad:dbdata”

  • In the above command, “RutaMongodb” is the folder in which we install and “Unit” is the letter of the disk where is the respective path to enter “Db / data”.
  • Next on the console screen we must locate the message “Waiting for connections”. This is a clear telling us that the service is working satisfactorily.
  • After starting the server you have to open another console and go back to the installation path of MongoDB. Once there you have to locate the called client exe and when you run it will seek to connect with our service.
  • On the other hand, if you don’t want to install MongoDB, you can access the online version of your website. In this we will see a MongoDB console created in JavaScript, which makes our tasks easier if we cannot install it on our computer.

Initial Data for the Puebas Environment

Once we can run MongoDB, we can also enter commands. But for this we need several test data that give us the option to evaluate the queries. To begin we have to leave them the records that they can copy and paste in their terminal. With this they can have something inside the Database to obtain results like the ones we will show you below

db.guiamongo.insert({"nombre": "Maria", "edad":"25", "genero":"Femenino",
 "pais":"Colombia" });db.guiamongo.insert({"nombre": "Pedro", "edad":"32", 
"genero":"Masculino", "pais":"Ecuador" });db.guiamongo.insert({"nombre": "Ramon", 
"edad":"18", "genero":"Masculino", "pais":"Honduras" });db.guiamongo.insert
({"nombre": "John", "edad":"22", "genero":"Masculino", "pais":"Argentina"
 });db.guiamongo.insert({"nombre": "Rosa", "edad":"45","genero":"Femenino",
 "pais":"Chile","idiomas":["Esp", "Ing", "Fra"] });

After you have entered all the data properly, the sandbox is complete.

Make Queries Using Find ()

In this section we will talk about the most important tool for query in MongoDB. It’s about the command Find () that allows us to defectively locate the documents necessary to carry out our work. In this case, these documents are similar to the records located in other programs that also manage databases.

To use Find () quickly you must pass an array with all the properties of the document to consult. This is enough for the return of everything that meets the respective established condition. Next we will show you an example in which we have to locate the documents where “gender” is “Female”. To carry it out you have to enter the following command in the console:


Then we will see in the console the way in which MongoDB returns the two records that we entered in the previous test environment.

Now we can see that the documents are complete and perhaps we may only need one attribute. If this is the case, just pass a second array to find () to indicate that we need to fetch from said array. In turn, by default the primary key_id. Next we will show you the command to consult only the names of the query made previously.

db.guiamongo.find({“genero”:”Femenino”}, {“nombre”:1});

Find () Command Restrictions

One of the main limitations of this command is that we do not have the option to search and make conditions with elements of the same file or document. As an example we have the following: we cannot compare the “age” field with another field that is numeric in the same document.

On the other hand, since there are no relationships between our documents, it is impossible to execute the so-called “joins”. That is why we cannot have a catalog table and only add the respective code to the first document.

We have already finished! With this little guide you can query in MongoDB quickly and without any inconvenience. See you in the next post!