How do you find stuff in Mongo DB?

5 minute read time.

Mongo DB (for those who do not know what it is) is a noSQL database. If you want to know more about noSQL I suggest google searching it. Same goes for Mongo DB.  Mongo DB has a huge community forum site, blog site, and training site for just about everything you could ask for. Majority of the options Mongo DB offers is free; so, take advantage of it while you can. Since Mongo DB is not a SQL defined database; traditional statements like SELECT or ORDER BY will not work. They use more similar calls to APIs like GET and FIND.  

So, where do we start? Where everyone else does; Mongo DB’s manual site.  (https://docs.mongodb.com/manual/mongo/)
Here you will get everything you need on how to start querying for data. The examples they list have a slight learning curve but after a while of trying them out you’ll get the hang of it. A good tip is to remember that Mongo DB is case sensitive. Be careful how you write and read. Some things will look totally wrong but, is not. If you’re like me, and you don’t like using windows command prompt or oracles BASH command, then you’ll want to get a GUI like Microsoft’s SQL Server Management Studio (SSMS). Unlike Microsoft, Mongo DB has a free developer version of Mongo DB. Therefore, developers have made hundreds of different GUI compatible interfaces for Mongo DB to use. I traditionally use Robomongo (https://robomongo.org) or Mongo Compass (if I want to be fancy). Robomongo has a free and a paid version that is available. The main differences are features but for the sake of the blog, the free will work nicely. Mongo Compass “full” is paid only unless you’re signed up as a developer then you get access to Compass Full. Compass is Mongo DB’s official GUI management studio for Mongo DB.  Mongo Compass Community is a lite free version of Compass (https://www.mongodb.com/download-center/compass?jmp=docs). All screenshots here will be of Robomongo.

Starting with Robomongo installed (which is not covered here in this blog) connect to the database.
Sage EM’s Database structure should look like this:


Note: The total number of Collections can vary depending of the version/patch of Sage EM you have installed.
We are going to start with a new query window. From here we are going to write and run the following command db.runCommand( { listCommands: 1 } )
Should get the following results.


This is the list of all acceptable commands allowed by the version of Mongo DB you have installed. If you further drill down into the commands most of them will include a help topic and a http link to Mongo’s site on how to use that specific command or additional information on the command.

There is also a more complete listing on Mongo’s site (https://docs.mongodb.com/manual/reference/command/).  A simple command is serverStatus command. It is used to tell you what the status of the Mongo DB server. It’s a super easy way to see the exact version of Mongo DB running. How you use it in its simplest form is:  db.runCommand( { serverStatus: 1 } )
The returned results should look like so:

Here you can see the versioning, lock status, network connections, uptime, and more. It’s a simple way to check on Mongo DB. There is also another status base command that is easily used and it allows you to view stats of a specific collection; db.runCommand({collStats: "Collection.Name"})  (Collection.Name is to be replaced with the name of the collection)
For example, if you ran the following:  db.runCommand({collStats: "Role"})  it would give you the collection (table) stats for that collection “Role”

Here you can see the collection size, document count, index count and more. You can read all about commands at the link I posted a little further up.
 
I have one more site for us to review. That is the Database Methods (https://docs.mongodb.com/manual/reference/method/js-database/). This is a listing of Method commands like getCollection and dropDatabase commands. Mongo Refers to these as Methods which the same links will explain. For the sake of our blog we will be using the getCollection. Its similar to SELECT in MS SQL. For our first and most basic query, we are going to load the "User" Collection (note: collections are what Mongo DB calls a Table of data). Write the following to the query window:  db.getCollection('User').find({})  It should return this type of results:

Essentially the query is saying select * from table
Looks like I only have 6 users in Mongo DB. The User collection ties directly to Sage EM’s Administration, Administration, Users, User function. Each user gains a _id label which is what we are seeing here. Drilling down into the _id keys you will see additional information. _id keys are the unique identifier for the document or record. As you see here, the document we see is the user ADMCA (note: documents in Mongo DB  are the same as records or rows in MS SQL)

We will continue with using ADMCA as our test. Let’s say we wanted to filter the list of users down to a specific user (ADMCA). Pretend I have a lot of users. When searching for a user you must know two things, the column (field) name and the value in which you need to locate. In this simplified case the column name is login and value are ADMCA. This is how you would write the query using the GET in conjunction with FIND.  db.getCollection('User').find({'login':'ADMCA'})  [note the case sensitivity]
This should filter the Collection “User” by logins with the value ADMCA

The same format can be used for any of the other collections if you know how to locate the two items you need. When searching through an array or compounded data you can include that in the breakdown of the find.  In our next example we will use the PageLayout collection to demonstrate how to search when you have compounded data or data you can drill down into where layers are involved.

In this case, we want to find out how many documents do we have in the PageLayout Collection that have a _variantType of landingPage. The way we can do that is tiering the search. We tier by adding a period or “.” Dot as a drill down segment when searching.  Example of this is here:  db.getCollection('PageLayout').find({'page._variantType':'landingPage'})

Note that I wrote the spelling exactly as it is in the collection even keeping the capital letters and the underscore “_”.

The returned value was 48 documents that had a _variantType of landingPage

Now you have a basic understanding of how to find stuff in Mongo DB. Do not forget to check out Mongo DB’s website for additional information on how METHODS and COMMANDS work with the environment.