MongoDB vs MySQL   
Published: 2021-07-18 04:04:11

After two full days of learning MongoDB, I can definitively state that the better of these two database systems is...completely opaque to me.

I'll first back up and say that most of yesterday was spent wrapping my head around querying a MongoDB model. How the heck to you create a model, populate it with some data, and then *at some later point*, query that data? That was a toughie.

I'll back up further and say that I still haven't used MongoDB directly. Instead, I've been interfacing with it via Mongoose, which, in my very not-solid understand today, is sort of like an ORM for MongoDB (Mongo from here on out).

Being sufficiently backed up, I'll describe two of the most practical things I've learned about Mongo in my short time using it and then talk a little bit about comparing it to *SQL.

##Querying Mongo(ose) Models
My journey started with Mongoose's [official docs](https://mongoosejs.com/docs/index.html).

Here I encountered the basics of creating a **schema**, using that schema to build a **model**, and then populating that model with a data into a record, or to use Mongo's parlance, a **document**.

That process is straightforward enough.

The next logical step then is to query your database for the the data - the document that you've inserted into it.

---
As a brief aside, I'll mention that I am approaching this from a learning experience. Rather than just plow into our HR sprint, I wanted to take some time to play with Mongo on its own. That's how I like to learn - by "getting the reps in" and building a hands-on sense - an intuition - for how a tool is supposed to work.

---

### Querying the Model

Alright so let's say we created the model 'Ball' (and perhaps that it has one entry, something like {material: 'rubber', shape: 'brown'}).

Let's query it from another module (with a db connection:
`Kitten.find(callback)`

and...

`Kitten not defined` (or something like that)

What's going on?

Well that was a mystery to me. Why doesn't the model have access to Kitten? Isn't Kitten saved - isn't that the point of a database system?

I did some searching but couldn't figure out how to query *from a different module than where the schema/model/document were created*.

###Mongoose is not Mongo

So here's the deal. All that I've talked about so far - that's been the Mongoose library, not Mongo. Mongoose talks to Mongo. And not understanding that was the source of my confusion.

When you write create a schema and build a Model out of that - you're not doing that in Mongo. You're giving Mongoose the info that it needs to perform your task.

So if you make a reference to your Ball model without explicitly defining to Mongoose which ball model exactly, it won't be able to search the database for the Ball model that you're looking to pull data from. And you explicitly define that model the same way you made it, by creating a schema (the exact same schema) and creating a model from that schema with the correct name.

If I was my own editor - and if I took my advice - I'd scrap 90% of what I wrote above and just show the code. Here's the code:

```javascript
//create_data.js

const mySchema = new mongoose.Schema({
  repo_name: String,
  owner_name: String,
  stargazers_count: Number,
  url: String,
  description: String
})

const Repo = mongoose.model('Repo', mySchema);

Repo.create({
name: item.name,
owner_name: item.owner.login,
stargazers_count: item.stargazers_count,
url: item.html_url,
description: item.description})
```

```javascript
//query_model.js

///same code!!!
const mySchema = new mongoose.Schema({
  repo_name: String,
  owner_name: String,
  stargazers_count: Number,
  url: String,
  description: String
})

///same code!!!
const Repo = mongoose.model('Repo', mySchema);

var docs = Repo.find({owner_name:'zbretz'}).exec()
  .then(function(result){
    console.log(result)
  })
```

To belabor the point: You need that schema+model info when you query the Database. That's how mongooses roll.