tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 No SQL > Mongo DB > How to insert a Document in Mongo Database3

How to insert a Document in Mongo Database3 

MongoDB is a scalable, high-performance, open source NoSQL database. It offers Ad hoc queries, Indexing, Replication, Load balancing, File storage (GridFS), Aggregation, MapReduce and Server-side JavaScript execution. This requires the library mongo-2.8.0.jar to be present in the classpath. The following example shows how to insert a document in Mongo database.

File Name  :  
com/bethecoder/tutorials/mongodb/tests/InsertCountriesTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.mongodb.tests;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class InsertCountriesTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    try {
      //Connect to mongoDB with given IP and PORT
      Mongo mongo = new Mongo("localhost"27017);
      
      //Get the database object
      DB database = mongo.getDB("mytestdb");
      
      //Gets a collection with a given name.
        //If the collection does not exist, a new collection is created.
      DBCollection collection = database.getCollection("mycountries");
      //collection.drop();
      
      collection.insert(getDocument("India""Andhra Pradesh"123456));
      collection.insert(getDocument("India""Karnataka"651234));
      
      collection.insert(getDocument("USA""Florida"786454));
      
      collection.insert(getDocument("China""Beijing"7954));
      collection.insert(getDocument("China""Yumen"96454));
      
      //Queries for all objects in this collection
      DBCursor dbCursor = collection.find();
      DBObject record = null;
      
      while (dbCursor.hasNext()) {
        record = dbCursor.next();
        System.out.println(record);
      }
      
    catch (UnknownHostException e) {
      e.printStackTrace();
    catch (MongoException e) {
      e.printStackTrace();
    }
  }
  
  public static DBObject getDocument(String country, String state, long population) {
    DBObject document = new BasicDBObject();
    document.put("country", country);
    document.put("state", state);
    document.put("population", population);
    return document;
  }
}
   

It gives the following output,
{ "_id" : { "$oid" : "503cb60b42e2d654aaaa7251"} , 
	"country" : "India" , "state" : "Andhra Pradesh" , "population" : 123456}
{ "_id" : { "$oid" : "503cb60b42e2d654aaaa7252"} , 
	"country" : "India" , "state" : "Karnataka" , "population" : 651234}
{ "_id" : { "$oid" : "503cb60b42e2d654aaaa7253"} , 
	"country" : "USA" , "state" : "Florida" , "population" : 786454}
{ "_id" : { "$oid" : "503cb60b42e2d654aaaa7254"} , 
	"country" : "China" , "state" : "Beijing" , "population" : 7954}
{ "_id" : { "$oid" : "503cb60b42e2d654aaaa7255"} , 
	"country" : "China" , "state" : "Yumen" , "population" : 96454}



 
  


  
bl  br