Building Your First Application on MongoDB ~ Metadata Catalog

I gave a webinar about building your first application on MongoDB on May 15, 2012. The presentation began with a brief overview of MongoDB, jumped into a possible use-case (creating a metadata catalog), and then I jumped into the MongoDB shell to show some live CRUD operations. My slides are posted below, and I've also added some of the sample queries you can run with the MongoDB shell. Let me know if you have any questions!

 

 

 

If you're running MongoDB on your laptop, and you'd like to start playing around, you need to do two things. First, start up MongoDB. When I'm just messing around, I usually create a data directory and pass that in as a parameter. It's easy to delete afterwards. If you don't specify a data directory, it will put data into /data/db .

 

Now that MongoDB is started, start up the shell in another terminal tab. Just type the following and hit enter:

Now we're in the shell. We tell it what database to use. You can declare new ones on the fly. Let's tell it to use the "test123" database.

Let's say we wanted to store metadata about hospital patients. We might want all of this metadata to be stored in a "patients" collection. In MongoDB, we don't need to create a collection before inserting data. We specify the collection name when inserting our documents, and if the collection does not exist, it will be created for us. Let's insert a patient document.

Done. Easy. No schema required. If we want to verify that the document was inserted, we just run the following:

If we want to have it format nicely, we can add .pretty() to the end of the find(). We can insert another document, and it doesn't need to have the same schema:

We have two documents in the database. They don't have all the same fields, but because MongoDB does not enforce a schema, this is not a problem. We can test out some of the basic queries in MongoDB with the following:

Only Katy's document should return. While this will be fast since we only have two documents in the database, keep in mind that it is doing a table scan to find the document. If you want to get started with indexing in MongoDB to get faster queries, check here. Now we have two documents in MongoDB, and we can update them with MongoDB's atomic operations. Let's give Katy a birthday.

In terms of CRUD, we've gotten the CREATE, READ, and UPDATE portion done. Let's delete a document.

Bye bye, Katy Fong! In any case, hopefully that's enough to get you started with building your first application in MongoDB. Use the MongoDB shell to help model data, insert sample data, and test some queries. Once you want to wire it up to a real application, give some of the language specific drivers a shot! Let me know if you have any questions.

One thought on “Building Your First Application on MongoDB ~ Metadata Catalog

  1. You should gain access right into the current MongoDB server. Now you can run Mongo shell commands to create databases, collections, store new data, or edit old data entries.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">