Download the following code to start coding with Java and MongoDB.
To connect database, specify database name, if database doesn't exist then mongodb creates it automatically.
import com.mongodb.DB; import com.mongodb.MongoClient; public class Main{ public static void main( String args[] ){ try{ MongoClient mongoClient = new MongoClient( "localhost" , 27017 ); DB db = mongoClient.getDB( "test" ); System.out.println("Connect to database successfully"); boolean auth = db.authenticate("myUser", "myPassword".toCharArray()); System.out.println("Authentication: "+auth); }catch(Exception e){ System.err.println( e.getClass().getName() + ": " + e.getMessage() ); } } }
To create a collection, use the createCollection() method from com.mongodb.DB class.
import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) { try { MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("test"); System.out.println("Connect to database successfully"); boolean auth = db.authenticate("myUserName", "myPassword".toCharArray()); System.out.println("Authentication: " + auth); DBCollection coll = db.createCollection("mycol",null); System.out.println("Collection created successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } }
To get/select a collection from the database, use getCollection() method from com.mongodb.DBCollection class.
import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) { try { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); System.out.println("Connect to database successfully"); boolean auth = db.authenticate("myUserName", "myPassword".toCharArray()); System.out.println("Authentication: " + auth); DBCollection coll = db.createCollection("mycol", null); System.out.println("Collection created successfully"); coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } }
To insert a document into mongodb, use insert() method from DBCollection class.
import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) { try { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); System.out.println("Connect to database successfully"); boolean auth = db.authenticate("myUserName", "myPassword".toCharArray()); System.out.println("Authentication: " + auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); BasicDBObject doc = new BasicDBObject("title", "MongoDB") .append("description", "database") .append("likes", 100) .append("url", "http://www.java2s.com"); coll.insert(doc); System.out.println("Document inserted successfully"); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } } }
To select all documents from the collection, use find() method from com.mongodb.DBCollection class.
This method returns a cursor, and we can iterate this cursor.
import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); boolean auth = db .authenticate("myUserName", "myPassword".toCharArray()); DBCollection coll = db.getCollection("mycol"); DBCursor cursor = coll.find(); int i = 1; while (cursor.hasNext()) { System.out.println("Inserted Document: " + i); System.out.println(cursor.next()); i++; } } }
To update document from the collection, use update() method from com.mongodb.DBCollection class.
import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); boolean auth = db .authenticate("myUserName", "myPassword".toCharArray()); System.out.println("Authentication: " + auth); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); DBCursor cursor = coll.find(); while (cursor.hasNext()) { DBObject updateDocument = cursor.next(); updateDocument.put("likes", "200"); coll.save(updateDocument); } System.out.println("Document updated successfully"); cursor = coll.find(); int i = 1; while (cursor.hasNext()) { System.out.println("Updated Document: " + i); System.out.println(cursor.next()); i++; } } }
To delete first document from the collection, select the documents using findOne() method and use remove method from com.mongodb.DBCollection class.
import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; public class Main { public static void main(String args[]) throws Exception { MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("test"); boolean auth = db .authenticate("myUserName", "myPassword".toCharArray()); DBCollection coll = db.getCollection("mycol"); System.out.println("Collection mycol selected successfully"); DBObject myDoc = coll.findOne(); coll.remove(myDoc); DBCursor cursor = coll.find(); int i = 1; while (cursor.hasNext()) { System.out.println("Inserted Document: " + i); System.out.println(cursor.next()); i++; } System.out.println("Document deleted successfully"); } }