Introduction to MongoDB

Introduction to MongoDB Deconstructing the Mongoose Document

Learning Paths:
Topics:
Check out a free preview of the full Introduction to MongoDB course:
The "Deconstructing the Mongoose Document" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:

Scott reviews what the object id is, the versioning of Schemas in the Mongoose document, and the difference between a Mongoose document and a JavaScript object. A question is also asked regarding how Mongoose differs from Mongo.

Get Unlimited Access Now

Transcript from the "Deconstructing the Mongoose Document" Lesson

[00:00:00]
>> Scott Moss: The first thing is you see this _id. By default Mongo is gonna create _id for every single document that you save in the database by default. You can turn that off and you can make it do whatever you want. But my default it's gonna be _id, and its value is gonna be something that's called an object id.

[00:00:18] Who here has heard about object id before? Yeah, me neither when I first used Mongo. I was like, what is that? It looks like a string or it looks like a number or a string to me, what is that? Well object id is something from this package called BSON, B-S-O-N, if you've never heard of that, that's something Mongo uses and we can talk about that later.

[00:00:36] But it's basically an object representation of a unique id, and that's what Mongo uses for its ids. And they're very recognizable. I can look at a Mongo id and I can tell you that this person uses a Mongo database. I see this in the real world I'm like, they use Mongo, cuz they're very recognizable.

[00:00:53] It's not like most databases where there's a counter and it goes up, right, it's not like that. It's a unique, guaranteed unique id that's object based, and that's what's called an object id. And that's gonna be generated for every single document that you create. So you don't have to worry about figuring out the primary key or the ID of a document, it does it for you.

[00:01:13] You just create the document and then you get an id, you're good to go. The next thing to note is obviously the first name cuz that's what our student had was a first name, so we got that. Then you get this _ _v0, this you will probably almost will never use.

[00:01:30] This has to do with the version of the schema. If you're versioning schemas, you might be asking yourself, of course I'm going to version my schemas and I'm gonna have this. Most likely you won't use this. I've never had to use this in production. In fact I always strip this version thing out because the way you wouldversion you schemas is if you do that you would probably handle the versioning yourself and not allow Mongo handle it.

[00:01:52] You would just handle the version yourself with migrations or what ever approach you wanna do. So you wouldn't have this thing internally do it, and you wouldn't check versions on here. So most likely you would never use this but it's here, that's why you see that. And by default, that's everything you're gonna get for creating a document.

[00:02:07] If we had more fields, obviously, we would see more fields here. But all we have is a first name and its value is Tim, and that's in the database. Yes?
>> Speaker 2: So does Mongo have some validation kind of, that's why it has it, it has some validation that it has to validate instead of this version I'm giving you this data?

[00:02:21] If you had different versions you would probably give different set of data for different fields, whatever, right?
>> Scott Moss: Well-
>> Speaker 2: I don't know
>> Scott Moss: Yes and no. So Mongo itself doesn't have validations, but you can use validations and we will. But where that happens, it's not on a database level.

[00:02:36] It happens outside of the database. And then this versioning right here, it's misconceiving. It's not gonna allow you to say, save this student with this schema version and save this student with this schema version, it's just not gonna work that way. That's why I was like it's kinda pointless.

[00:02:52] Cuz it's like, what is the point of this? It doesn't really make any sense, cuz you can't do that. That's just a whole other schema that you would make. So yeah, this right here, I think this was part of Mongo a long time ago, when it made sense.

[00:03:04] But now, it just doesn't really make sense, they just never got rid of it.
>> Scott Moss: Cool, the other thing to take note of is that, you can't tell by the log, but this student object is not a regular JavaScript object. It's what we call a Mongo document. There's a whole bunch of stuff on here that's not logging out.

[00:03:25] You can't see it because it's not innumerable. When you do a log in JavaScript, the only thing that logs are properties that are innumerable, and the properties that I want to talk to you about are not innumerable. So when I log them, you won't see them. But there's other properties on this because it itself is not a JavaScript object, it's an object from the database that has methods and all different types of things on it that we can use to operate on it.

[00:03:50] But you won't be able to see it because, like I said, it's not innumerable. Sometimes that's not desirable and you wanna convert that object to JSON. So in later exercises we'll do that, because if you start doing matching, fills, and id's, those properties can get in the way and kind of mess things up.

[00:04:06] It also slows down the database if you're passing around these objects in memory and you really just wanna pass around regular JavaScript objects and not document objects. So that's just a quick note. We'll run into this later, just wanted talk about it for a little bit. Yes?
>> Speaker 3: So this is a Mongoose document?

[00:04:23]
>> Scott Moss: Yes.
>> Speaker 3: Not a Mongo document?
>> Scott Moss: Did I say Mongo? Yeah, Mongoose document. Sorry, yeah, it's a Mongoose document.
>> Speaker 4: How are they different?
>> Scott Moss: How are they different? Yeah, they're different because, so if you can imagine, you have Mongo right here, right, and then you have Mongoose sitting on top of it.

[00:04:37] Mongo by itself just spits JSON, that's all it does. It doesn't do anything else. It saves JSON, it spits out JSON. Mongoose sits on top of that and provides all different types of tools and methods and helpers that virtualizes that to make it easy for you. So the distinction is we call it a Mongoose document because it is in fact Mongoose that's taking that JSON from the database and adding all these methods and helpers and properties on it.

[00:05:04] And then providing it to you so you can operate on it within the confines of Mongoose. Mongo doesn't do that, it's just like here's JSON. So yeah, it is a Mongoose document, because if you didn't use Mongoose you wouldn't get this, you would just get JSON.