Develop In Java
  • Home
  • News
  • Tips and Tricks
  • Articles
    • Books>
      • Alfresco Enterprise Content Management Implementation
        • Beginning Java EE 6 Platform with GlassFish 3
          • EJB 3.1 Cookbook
            • GlassFish Security
              • GWT Java Ajax Programming
                • Java EE 5 Development using GlassFish Application Server
                  • JBoss RichFaces
                    • Seam 2.x Web Development
                    • Desktop Java>
                      • An Overview of The New Features in J2SE 5.0
                        • Using FindBugs with NetBeans 5.5
                        • Enterprise Java>
                          • Basic Java CRUD Operations With MongoDB
                            • Continuous Integration with Hudson on Glassfish
                              • Creating a Simple Spring MVC Web App With NetBeans 5.0
                                • Creating a Simple Spring MVC Web App With NetBeans 5.0 Part 2
                                  • Creating a Weld Project using Maven and NetBeans
                                    • Deploying a Java EE Web Application to OpenShift Express
                                      • Developing your first Web Service with Java EE 5 and NetBeans
                                        • Introduction to JSF 2 Using NetBeans and GlassFish
                                          • Securing a Web Application on Glassfish using JAAS
                                            • Securing a Web Application on Glassfish using JAAS Part 2
                                          • Links
                                          • Forums
                                          • Search
                                          • Contact Us

                                          Basic Java CRUD Operations with MongoDB

                                          In this post I’d like to show how to perform basic CRUD operations against a MongoDB database using the Java driver.
                                          For this post, lets assume that we have a todo database with a collection of todo items. Each item has a task and a priority.
                                          In terms of JSON notation, an example item would look like:
                                          { 
                                            "_id" : { "$oid" : "4bffb75121eec88a67ff6ec8"} , 
                                            "task" : "Write Code" , 
                                            "priority" : "high"
                                          }
                                          
                                          Now that we have defined what we are storing in the database, lets have a look at how we connect to Mongo.

                                          Connection to the database

                                          To connect to a MongoDB database, we would use code similar to that below. In this code you can see that we are connecting to a database called todo and getting the collection called items. In MongoDB if neither of these items exist, they will be automatically created.
                                          Mongo mongo = new Mongo(); 
                                          DB db = mongo.getDB("todo"); 
                                          DBCollection items = db.getCollection("items");
                                          

                                          Creating Documents

                                          To add a document to a collection, we use the insert() method of the collection.
                                          BasicDBObject doc1 = new BasicDBObject(); 
                                          doc1.put("task", "Write Code"); 
                                          doc1.put("priority", "high"); 
                                          items.insert(doc1);
                                          

                                          Retrieving Documents

                                          To retrieve documents from a collection, we can create a query and then iterate through it with a cursor.
                                          BasicDBObject query = new BasicDBObject(); 
                                          query.put("priority", "highest"); 
                                          DBCursor cursor = items.find(query); 
                                          // Print out "highest" priority items 
                                          while (cursor.hasNext()) { 
                                              System.out.println(cursor.next()); 
                                          }
                                          
                                          This query will find all the objects in the collection that have a priority of highest. If we wanted to get all of the items in the collection, we would create the cursor without a query as shown below.
                                          DBCursor cursor = items.find();
                                          

                                          Updating Documents

                                          To update an object, we first have to get the object from the collection then we save it back into the collection.
                                          BasicDBObject findTestItemQuery = new BasicDBObject(); 
                                          findTestItemQuery.put("task", "Test Code"); 
                                          DBCursor testItemsCursor = items.find(findTestItemQuery); 
                                          if(testItemsCursor.hasNext()) { 
                                              DBObject testCodeItem = testItemsCursor.next(); 
                                              testCodeItem.put("task", "Test and Review Code"); 
                                              items.save(testCodeItem); 
                                          }
                                          

                                          Deleting Documents

                                          Finally, to delete a document or set of documents, we use the remove method of the collection.
                                          BasicDBObject deleteQuery = new BasicDBObject(); 
                                          deleteQuery.put("priority", "highest"); 
                                          DBCursor cursor = items.find(deleteQuery); 
                                          while (cursor.hasNext()) { 
                                              DBObject item = cursor.next(); 
                                              items.remove(item); 
                                          }
                                          
                                          This article was originally published at davidsalter.co.uk
                                          You can discuss this post here.

                                          Locations of visitors to this page

                                          Copyright (C) DevelopInJava.com 2006-2012