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!
[crayon nums=”false”]mongod –dbpath /your-data/directory/here[/crayon]
Now that MongoDB is started, start up the shell in another terminal tab. Just type the following and hit enter:
[crayon nums=”false”]mongo[/crayon]
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.
[crayon nums=”false”]use test123[/crayon]
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.
[crayon nums=”false”]db.patients.insert({name: “Kevin Hanson”, birthday:” May 26, 1983″, city: “San Francisco”, });[/crayon]
Done. Easy. No schema required. If we want to verify that the document was inserted, we just run the following: [crayon nums=”false”]db.patients.find();[/crayon]
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:
[crayon nums=”false”]db.patients.insert({name: “Katy Fong”, city: “Berkeley”, ailment: “hypochondria”});[/crayon]
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:
[crayon nums=”false”]db.patients.find({city:”Berkeley”});[/crayon]
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.
[crayon nums=”false”]db.patients.update({name:”Katy Fong”},{$set:{birthday:”December 17, 1984″}});[/crayon]
In terms of CRUD, we’ve gotten the CREATE, READ, and UPDATE portion done. Let’s delete a document.
[crayon nums=”false”]db.patients.remove({name:”Katy Fong”});[/crayon]
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.
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.