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 Database2

How to insert a Document in Mongo Database2 

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/InsertToCollectionTest2.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 InsertToCollectionTest2 {

  /**
   @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("mynums");
      //collection.drop();
      
      //Create and insert document to collection
      DBObject document = null;
      int num = 0;
      
      for (int i = ; i < 20 ; i ++) {
        num = i + 1;
        document = new BasicDBObject();
        
        //Assign your own id for this document
        document.put("_id", num);
        document.put("num", num);
        document.put("even"(num % == 0));
        document.put("num^2", Math.pow(num, 2));
        
        //Saves a document to the database
        collection.insert(document);
      }
      
      //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();
    }
  }
}
   

It gives the following output,
{ "_id" : 1 , "num" : 1 , "even" : false , "num^2" : 1.0}
{ "_id" : 2 , "num" : 2 , "even" : true , "num^2" : 4.0}
{ "_id" : 3 , "num" : 3 , "even" : false , "num^2" : 9.0}
{ "_id" : 4 , "num" : 4 , "even" : true , "num^2" : 16.0}
{ "_id" : 5 , "num" : 5 , "even" : false , "num^2" : 25.0}
{ "_id" : 6 , "num" : 6 , "even" : true , "num^2" : 36.0}
{ "_id" : 7 , "num" : 7 , "even" : false , "num^2" : 49.0}
{ "_id" : 8 , "num" : 8 , "even" : true , "num^2" : 64.0}
{ "_id" : 9 , "num" : 9 , "even" : false , "num^2" : 81.0}
{ "_id" : 10 , "num" : 10 , "even" : true , "num^2" : 100.0}
{ "_id" : 11 , "num" : 11 , "even" : false , "num^2" : 121.0}
{ "_id" : 12 , "num" : 12 , "even" : true , "num^2" : 144.0}
{ "_id" : 13 , "num" : 13 , "even" : false , "num^2" : 169.0}
{ "_id" : 14 , "num" : 14 , "even" : true , "num^2" : 196.0}
{ "_id" : 15 , "num" : 15 , "even" : false , "num^2" : 225.0}
{ "_id" : 16 , "num" : 16 , "even" : true , "num^2" : 256.0}
{ "_id" : 17 , "num" : 17 , "even" : false , "num^2" : 289.0}
{ "_id" : 18 , "num" : 18 , "even" : true , "num^2" : 324.0}
{ "_id" : 19 , "num" : 19 , "even" : false , "num^2" : 361.0}
{ "_id" : 20 , "num" : 20 , "even" : true , "num^2" : 400.0}



 
  


  
bl  br