Insert List/Array in collection In MongoDb in java




Contents of page >
  • Program to Insert List/Array in collection In MongoDb in java >


Program to Insert List/Array in collection In MongoDb in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert Array in collection In MongoDb
*/
import java.util.ArrayList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class InsertArrayInDocumentInCollectionInMongoDb6 {
   public static void main(String[] args) {
          /* Connect to MongoDB */
          MongoClient mongoClient =
                       new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("employee");
          /*If collection already exists, you may remove everything
          from collection for perfect OUTPUT of program*/
          dbCollection.remove(new BasicDBObject()); //Not Mandatory
                      
          /** create a BasicDBObject (document /record) */
          System.out.println("create a BasicDBObject");
          BasicDBObject basicDBObjectMain = new BasicDBObject();
          basicDBObjectMain.put("id", 1);
          basicDBObjectMain.put("name", "Ankit");
          /** create a list of Phone */
          System.out.println("create a list of Phone");
          java.util.List<Integer> phoneList = new ArrayList<Integer>();
          phoneList.add(9899);
          phoneList.add(1234);
          /* Put phoneList in BasicDBObject */
          System.out.println("Put phoneList in BasicDBObject");
          basicDBObjectMain.put("Phone", phoneList);
         
          /** INSERT document/record in COLLECTION in MongoDB */
          System.out.println("INSERT document >");
          dbCollection.insert(basicDBObjectMain);
          /* Display documents of COLLECTION in MongoDB */
          System.out.println("Display documents of COLLECTION in MongoDB >");
          DBCursor dbCursor = dbCollection.find();
          while (dbCursor.hasNext()) {
                 System.out.println(dbCursor.next());
          }
  
   }
}
/* OUTPUT
create a BasicDBObject
create a list of Phone
Put phoneList in BasicDBObject
INSERT document >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "58738fba02860c3b50a26152"} , "id" : 1 , "name" : "Ankit" , "Phone" : [ 9899 , 1234]}
*/



Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR



Summary -
So in this MongoDB tutorial we learned with example and program how to to Insert list/Array in collection In MongoDb in java.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.



RELATED LINKS>






eEdit
Must read for you :