Contents of page >
- Program to Display Collection in MongoDb In Java
Program to Display Collection in MongoDb In Java
package insertDocumentInCollectionInMongoDB;
/**
* Write a program to Display Collection in MongoDb In Java
*/
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 DisplayCollectionInMongoDbInJava {
public static void main(String[] args) {
/* Step-1 > Connect to MongoDB */
System.out.println("Step-1 > Connect to MongoDB");
MongoClient mongoClient =
new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
/* Step-2 > Connect to DATABASE */
//If in case database doesn't exists, it will be created at runtime
System.out.println("Step-2 > Connect to DATABASE");
DB db = mongoClient.getDB("mydb");
/* Step-3 > Get the COLLECTION (TABLE) */
//If in case collection (Table) doesn't exists, it will be created at runtime
System.out.println("Step-3 > Get the COLLECTION (TABLE)");
DBCollection dbCollection = db.getCollection("employee");
/* Step-4 > Not Mandatory
If collection already exists, you may remove everything
from collection for perfect OUTPUT of program*/
System.out.println("Step-4 > Not Mandatory");
dbCollection.remove(new BasicDBObject());
BasicDBObject basicDBObject1 = new BasicDBObject();
basicDBObject1.put("id", 1); //Id is the column name, 1 is the value of column
basicDBObject1.put("name", "Ankit");
/* Step5 > INSERT document1/record1 in COLLECTION in MongoDB */
System.out.println("Step-5 > INSERT document1/record1 in COLLECTION in MongoDB");
dbCollection.insert(basicDBObject1);
BasicDBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("id", 2); //Id is the column name, 2 is the value of column
basicDBObject2.put("name", "Sam");
/* Step-6 > INSERT document2/record2 in COLLECTION in MongoDB */
System.out.println("Step-6 > INSERT document1/record1 in COLLECTION in MongoDB");
dbCollection.insert(basicDBObject2);
/** Step-7 > Display documents of COLLECTION in MongoDB */
System.out.println("Step-7 > Display documents of COLLECTION in MongoDB");
DBCursor dbCursor = dbCollection.find();
while (dbCursor.hasNext()) {
System.out.println(dbCursor.next());
}
}
}
/*OUTPUT
Step-1 > Connect to MongoDB
Step-2 > Connect to DATABASE
Step-3 > Get the COLLECTION (TABLE)
Step-4 > Not Mandatory
Step-5 > INSERT document1/record1 in COLLECTION in MongoDB
Step-6 > INSERT document1/record1 in COLLECTION in MongoDB
Step-7 > Display documents of COLLECTION in MongoDB
{ "_id" : { "$oid" : "585d012de1bb4d22d80c11a0"} , "id" : 1 , "name" : "Ankit"}
{ "_id" : { "$oid" : "585d012de1bb4d22d80c11a1"} , "id" : 2 , "name" : "Sam"}
*/
|
Summary -
So in this MongoDB tutorial we learned with example and program to how to Display 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>
Labels:
MongoDB Java