Skip to content

MongoDB Queries in a Nutshell

Emidio Piermarini edited this page Jan 11, 2018 · 27 revisions

Introduction

This wikipage will attempt to get you the 'low hanging fruit' in with MongoDB. With the following commands, you can do 80% of what a typical BH engineer will have to do with their data. If you want further information, please see MongoDB's online documentation: MongoDB Reference Manual.

As you will see, MongoDB's computer language is fairly easy to learn. Whilst it isn't extremely human readable, it is certainly more straightforward than things like nested loops. All of MongoDB's language is based on JSON syntax. It is worth looking a bit at the sytanx before jumping into Mongo Queries. Simply speaking, JSON script will usually look like this:

{"Key": Value} or {"Mongo Query Name": Stuff to Do}

Ok, now let's do some examples. You'll need to do the following:

  1. Open an instance of MongoShell (Details Here)
  2. Open Robo3T (Details Here)

Important Queries

aggregate

The $aggregate function that executes a list of queries one after the other. So let's break that down with what we already know:

Queries = JSON objects Queries = Programmatic Sieves Programmatic Sieves = Filters for us to get to the #DataWeWant

Therefore, by the transitive propery, the aggregate function takes in a list of JSONs and spits out the the subset of data from the database that we care about a.k.a the #DataWeWant.

Aggregate(database,List of JSONs) = #DataWeWant from database

When you use the grasshopper mongo tools, this is what is going on behind the scenes. Recalling that all queries are JSON objects, we should expect to see that the aggregate command takes in a set of JSON objects. To create this list, we will separate each query by a comma:

Aggregate(database,JSON1,JSON2,JSON3,...,JSON9999999) = #DataWeWant from database

Ok, so how do we actually execute this on our Mongo Database? Well, you could use the MongoShell. Or you could use the GrasshopperToolkit tools. But one of the easiest mediums to debug you code is Robo3T. This is because Robot3T has a skosh of intellesense that can help you understand where you have syntax errors in your aggregate command. Recall that if you execute an aggregate command in Robo3T and it works, you can feed the exact same list of queries into Grasshopper using the Mongo Components and it will yield the same result. Therefore, Robo3T is your best friend when you are just getting started with Mongo.

Let's set up our aggregate function. First, we open a collection:

As you can see, when I open my collection, the default command Robot3T throws in is the find() command. If we want to play with $aggregate commands, we need to remove this:

db.getCollection('Model A Forces Database').find({}) --------> db.getCollection('Model A Forces Database')

And in its play add the aggregate command:

db.getCollection('Model A Forces Database').aggregate([ ])

$project

{$project: {FX: 1}}

$match

$group

Clone this wiki locally