From the course: Learning MongoDB
Create a document - MongoDB Tutorial
From the course: Learning MongoDB
Create a document
- [Instructor] So what exactly are documents? Simply put, MongoDB documents are field-value pairs stored in a JSON-like format called BSON, which stands for Binary JSON. So what are you waiting for? Let's create some documents and save them to our database. Throughout this course, you'll see me using MongoDB shell on a terminal on a Mac. However the same terminal commands can all be run within Codespaces as well. To do that, you can start a New Terminal in the Codespace and right here I can list contents. I can go into the lessons folder. I can also run mongosh to get a MongoDB shell. So anywhere you're seeing me running a shell on a command line terminal, you can do the exact same thing in a Codespace terminal as well. What I'm going to go ahead and do is go into our course repo and go in the lessons folder and so lessons/2.1 for this lesson. And in here you can see there's a recipe.json file. I'm going to go ahead and open this in VS Code 'cause it's going to format it for us nicely. You can see something that looks a lot like JSON because it pretty much is JSON. Line two here, we go ahead and start with the title of a recipe for chicken tacos as well as a description and a cook time. You can see these two are strings and this one is integer and they're all raft in these curly brackets. So there can be multiple key value pairs and they can actually be all kinds of different data types, strings, rays, objects, more than that in future lesson. Let's go ahead and start inserting some documents into our database. So I'm going to go ahead and load up Mongo. Now this is the Mongo shell command. It might throw some warnings at you here. Don't worry about them. You can just clear them out of the way. If you had any problems and that didn't load up make sure to go back to the install video where we learned how to run the Mongod command to launch MongoDB server. Once we're in Mongo, we'll get a MongoDB shell. One important thing to know is this is a JavaScript shell. We can literally write JavaScript here if we want. But first let's connect to the right database. Again make sure to check back to the install videos if you haven't imported the example databases yet. So we're going to go ahead and use the cooker database and we'll do much like we did in the code editor but here we'll sign the document to a variable. So let's just call it doc, we could actually call it whatever we want and we'll give it a talk title and the title of Tacos and we'll add a description here of tacos. And we'll give it a cook time as well. And let's say that cook time is 20 minutes total and we're going to close it out right there and it's going to spit back something here. This means it worked correctly and it'll give us a nice formatted version of it. Now a document is a lot like a row and a sequel table, but it's way more flexible. So the next thing we'll do now that we have our document ready we'll need to insert it into a collection. So the way we do that is db. whatever our collection name is. Just call us tacos and then we're going to use this insertOne command and we're going to give it our variable and it's going to tell us properly inserted the document. If the collection didn't exist already it's actually automatically created for you, more in collections later but they're kind of analogous to tables in sequel. Let's go ahead and see what our document looks like. So let's do db.tacos.find, and we'll get back our document, the title, description, and cook time. But you'll also notice this other field here. This underscore ID, that wasn't in original document. The underscore ID field is required in every document in MongaDB and why you could supply your own. It's better to let MongoDB auto to generate one like it did here. Not only will make sure it's unique which it has to be, but also it actually contains an encoded daytime for you which is really useful for sorting. Anyhow, back to our document. There's also a little trick here that we can use on find and we can put this "pretty" at the end and it'll give us a nicely formatted document. And one more trick, if you press the up arrow in the shell you'll see we can get back previous commands. So if we wanted to assign another taco let's just call it document two and we'll call this Dos Tacos and then we can press up till we find that insert one command. We'll change the variable here to doc2 and then we will run the fined pretty, we'll get back two different documents, our Tacos and our Dos Tacos. In the next lesson we'll talk more about find and how to query documents.