Contents of page >
1) Program to Insert Document In Collection In MongoDb using BasicDBObject in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert Document In Collection In MongoDb in java using BasicDBObject
*/
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 InsertDocumentInCollectionInMongoDb1_BasicDBObject {
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 BasicDBObject1 (document /record) */
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1); //Id is the column name, 1 is the value of column
basicDBObject1.put("name", "Ankit");
/** INSERT document1/record1 in COLLECTION in MongoDB */
System.out.println("INSERT document1 >");
dbCollection.insert(basicDBObject1);
/** create a BasicDBObject2 (document /record) */
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2); //Id is the column name, 2 is the value of column
basicDBObject2.put("name", "Sam");
/** INSERT document2/record2 in COLLECTION in MongoDB */
System.out.println("INSERT document2 >");
dbCollection.insert(basicDBObject2);
/* 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
INSERT document1 >
INSERT document2 >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "58694be5f5bc383014042449"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "58694be5f5bc38301404244a"} , "id" : 2 , "name" : "Sam"}
*/
|
Download >
Read : How to Import Maven project in eclipse
OR
Also read : DELETE Document In Collection In MongoDb in java
UPDATE Document In Collection In MongoDb in java
FIND/SEARCH/Query/Display/Select from Collection In MongoDb in java
2) Program to Insert JSON In Collection In MongoDb in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert JSON In Collection In MongoDb in java
*/
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.util.JSON;
public class InsertDocumentInCollectionInMongoDb2_Json {
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 JSON 1 */
String json1= "{id : 1, firstName:'ankit'}";
DBObject dbObject1 = (DBObject)JSON.parse(json1);
/** INSERT JSON1 in COLLECTION in MongoDB */
System.out.println("INSERT JSON1 >");
dbCollection.insert(dbObject1);
/** Create JSON 2 */
String json2= "{id : 2, firstName:'sam'}";
DBObject dbObject2 = (DBObject)JSON.parse(json2);
/** INSERT JSON2 in COLLECTION in MongoDB */
System.out.println("INSERT JSON1 >");
dbCollection.insert(dbObject2);
/* Display documents of COLLECTION in MongoDB */
System.out.println("Display documents of COLLECTION in MongoDB >");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}
/* OUTPUT
INSERT JSON1 >
INSERT JSON1 >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "585c102a7dbba63084e26c1c"} , "id" : 1 , "firstName" : "ankit"}
{ "_id" : { "$oid" : "585c102a7dbba63084e26c1d"} , "id" : 2 , "firstName" : "sam"}
*/
|
3) Program to Insert Document In Collection In MongoDb Using BasicDBObjectBuilder in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert Document In Collection In MongoDb in java Using BasicDBObjectBuilder
*/
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class InsertDocumentInCollectionInMongoDb3_UsingBasicDBObjectBuilder {
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 BasicDBObjectBuilder1 */
BasicDBObjectBuilder basicDBObjectBuilder1 = new BasicDBObjectBuilder();
basicDBObjectBuilder1.add("id", 1); //Id is the column name, 1 is the value of column
basicDBObjectBuilder1.add("name", "Ankit");
/** INSERT document1 in collection in MongoDB */
System.out.println("INSERT document1 >");
dbCollection.insert(basicDBObjectBuilder1.get());
/** create a BasicDBObjectBuilder 2 */
BasicDBObjectBuilder basicDBObjectBuilder2 = new BasicDBObjectBuilder();
basicDBObjectBuilder2.add("id", 2);
basicDBObjectBuilder2.add("name", "Sam");
/** INSERT document 2 in collection in MongoDB */
System.out.println("INSERT document2 >");
dbCollection.insert(basicDBObjectBuilder2.get());
/* Display documents of COLLECTION in MongoDB */
System.out.println("Display documents of COLLECTION in MongoDB >");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}
/* OUTPUT
INSERT document1 >
INSERT document2 >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "585c10347dbba6152c63f68b"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585c10347dbba6152c63f68c"} , "id" : 2 , "name" : "Sam"}
*/
|
4) Program to Insert Document In Collection In MongoDb Using Map And BasicDBObject in java >
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Insert Document In Collection In MongoDb in java Using Map And BasicDBObject
*/
import java.util.HashMap;
import java.util.Map;
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 InsertDocumentInCollectionInMongoDb4_UsingMapAndBasicDBObject {
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 Map 1 (document /record) */
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", 1); //Id is the column name, 1 is the value of column
map1.put("name", "Ankit");
/** INSERT document1/record1 in COLLECTION in MongoDB */
System.out.println("INSERT document1 >");
dbCollection.insert(new BasicDBObject(map1));
/** create a Map 2 (document /record) */
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", 2); //Id is the column name, 2 is the value of column
map2.put("name", "Sam");
/** INSERT document2/record2 in COLLECTION in MongoDB */
System.out.println("INSERT document1 >");
dbCollection.insert(new BasicDBObject(map2));
/* Display documents of COLLECTION in MongoDB */
System.out.println("Display documents of COLLECTION in MongoDB >");
DBCursor cursor = dbCollection.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
}
}
/* OUTPUT
INSERT document1 >
INSERT document1 >
Display documents of COLLECTION in MongoDB >
{ "_id" : { "$oid" : "585c103f7dbba62be85406ec"} , "name" : "Ankit" , "id" : 1}
{ "_id" : { "$oid" : "585c103f7dbba62be85406ed"} , "name" : "Sam" , "id" : 2}
*/
|
Download >
Read : How to Import Maven project in eclipse
OR
5) Program to Insert 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]}
*/
|
6) Data modelling in MongoDb in java (Multiple table - primary and foreign key).
Please read : Data modelling in MongoDb in java (Multiple table - primary and foreign key - one to one /one to many
Download >
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 Document 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>