Over a million developers have joined DZone.

Learning MongoDB: The CRUD

DZone's Guide to

Learning MongoDB: The CRUD

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

If you have not been following my blog for the training I am undertaking on MongoDB, it is time to do so now because this is getting way too interesting. Last night I took the Week 2 course and it was a slam dunk. I opened the course at around midnight and I wanted to finish the same day that I wanted to submit my homework and get on the scorecard. It took me around 3 hours and honestly I skipped the last 5 Lectures. I realized that a) I should not be doing this past midnight as it gets real hard to focus when you're wanting to sleep and b) should not have a movie running on the side. Anyway, I managed to finish the course in just under 3 hours and it felt good.

What I Learned

Week 2 is about CRUD, which in Mongo terms is Insert, Find, Update, Remove (they didn’t have a cool acronym like CRUD). The first half of the course was heavy in mongoshell and working with various commands. The 2nd half of the course moved over into Java and we were asked to do similar operations using the Java driver for Mongo.  There were no frameworks like Spring and it was all native Mongo. To list down all that I learned:

a) insert()

b) findOne() and find()

c) update() (including multi updates)

d) remove()

e) Query using $gt, $lt, regex, $exists, $type, $or, $and, arrays, $in, $all, $set, $unset, $push, $pop, $pushAll, $pullAll

f) Cursors and find, sort, limit (paginate)

When I tell you that this week will come as a big surprise and it is heavy considered to what Week 1 is // so be prepared to focus.

My Takeaways

This week we did not get into modelling where I really want to go to, but this week was insightful as it allowed me to understand the power of Mongo’s schemaless design principle. There are ways in Mongo that we can get data and only get what we need, we can even set what we need. So the need to work on various joins and managing schemas as the domain grows is really pretty easy in Mongo. We have been looking to use mongo in a setting to manage data for various tenants, and the profile itself will vary not just from  tenant to tenant, but also between tenants as they might work with multiple profiles. Also, at times we may just need to update profiles. Last night provided me with a good understanding of how we can meet all those uses and not deal with a lot of Java boilerplate code.  We'll just delegate the stuff/tasks over to Mongo and let it do it for us.

The other big takeaway was that working with Mongo’s document structure is much simpler than RDBMS. While was I working in mongoshell where I was using JavaScirpt to write code, and later, when I moved over to Java classes it was fairly simple. It was all about knowing 3 classes (if not 2) and you are done. Working with Maps (or Dictionaries) is pretty simple and using that knowledge in any language can't really be difficult. I was able to go back and learn JavaSciprt all over again and code a logic pretty quick.

Key Takeaways

a) The flexibility that MongoDB brings with itself is pure awesomeness and amazing. It has the capability to get anyone started in matter of minutes.

b) The operations and way they work on the underlying documents is pretty slick and present several opportunities – must try.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}