Mongo DB connector – Java code
MongoDB (from "humongous") is an open source document-oriented database system developed and supported by 10gen. It is part of the NoSQLfamily of database
systems. Instead of storing data in tables as is done in a
"classical" relational database, MongoDB stores
structured data as JSON-like documents
with dynamic schemas (MongoDB calls the format BSON), making the
integration of data in certain types of applications easier and faster.
Here is the Java code for making connection with
Mongo DB and creating documents over there.
/**
*
* @author Amal_Babu
*
*/
package com.examples;
import java.util.ArrayList;
import
com.mongodb.BasicDBObject;
import com.mongodb.DB;
import
com.mongodb.DBCollection;
import com.mongodb.Mongo;
import
com.mongodb.MongoException;
public class DBManager {
private static DB database;
public static void setDatabase(String
hostName, int port, String DBName)
throws Exception {
try {
Mongo
mongo = new Mongo(hostName, port);
database =
mongo.getDB(DBName);
System.out.println(database);
DBCollection
collName = database.getCollection("testData");
System.out.println(collName);
ArrayList<String>
arrayList_bid = null;
for (int i = 0; i < 10000;
i++) {
arrayList_bid
= new
ArrayList<String>();
arrayList_bid.add("pos1_" + i + 10);
arrayList_bid.add("pos2_" + i + 20);
arrayList_bid.add("pos3_" + i + 30);
BasicDBObject
doc1 = new BasicDBObject("bid", arrayList_bid);
collName.insert(doc1);
}
System.out.println(collName.findOne());
}
catch (MongoException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
setDatabase("172.22.91.32", 27017, "myDB");
}
}
No comments:
Post a Comment