MongoDB :
Data in MongoDB has a flexible schema. Collections do not enforce document structure. This means that:
- documents in the same collection do not need to have the same set of fields or structure, and
- common fields in a collection’s documents may hold different types of data.
Each document only needs to contain relevant fields to the entity or object that the document represents. In practice, most documents in a collection share a similar structure. Schema flexibility means that you can model your documents in MongoDB so that they can closely resemble and reflect application-level objects. For more information Click on that link.
Requirements :
- Download MongoDB from here.
- Install MongoDB and set the PATH variable .
- Install JDK 7 (The following code is Tested On JDK 7.
Note: Before Running the code, Need to start MongoDB server .
Following is the CODE to perform CRUD operation's of MongoDB with JAVA.
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Date;
import org.bson.types.ObjectId;
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.QueryBuilder;
import com.mongodb.ServerAddress;
public class MongoDBCRUDOperationInJava {
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));
DB courseDB = client.getDB("students");
DBCollection collection = courseDB.getCollection("grades");
//collection.drop();
//insertDocument(collection);
//findOne(collection);
//findAll(collection);
//count(collection);
//findWithCriteriaUsingDBObject(collection);
findWithCriteriaUsingQueryBuilder(collection);
}
public static void insertDocument(DBCollection collection) {
DBObject doc = new BasicDBObject("_id", new ObjectId());
doc.put("name", "Harmeet");
doc.put("age", 24);
doc.put("birthday", new Date(12312312313l));
doc.put("programmer", true);
doc.put("language", Arrays.asList("Java", "Scala", "Python"));
doc.put("address", new BasicDBObject("street", "5").append("area", "moga"));
DBObject doc2 = new BasicDBObject().append("name", "Jimmy");
System.out.println("Before Inserting the Document in Collection : "+doc);
collection.insert(Arrays.asList(doc, doc2));
System.out.println("After Inserting the Document in Collection : "+doc);
}
public static void findOne(DBCollection collection) {
DBObject doc = collection.findOne();
System.out.println(doc);
}
public static void findAll(DBCollection collection) {
DBCursor cur = collection.find();
try{
while(cur.hasNext()){
DBObject doc = cur.next();
System.out.println(doc);
}
}finally{
cur.close();
}
}
public static void count(DBCollection collection) {
System.out.println("COunt of collection Documents : "+collection.count());
}
public static void findWithCriteriaUsingDBObject(DBCollection collection) {
DBObject criteria = new BasicDBObject("type", "homework").append("student_id", 9);
DBCursor cur = collection.find(criteria);
try{
while(cur.hasNext()){
DBObject doc = cur.next();
System.out.println(doc);
}
}finally{
cur.close();
}
}
public static void findWithCriteriaUsingQueryBuilder(DBCollection collection) {
QueryBuilder builer = QueryBuilder.start("type").is("homework").and("student_id").is(9).and("score").greaterThan(60);
DBCursor cur = collection.find(builer.get(), new BasicDBObject("student_id" , true).append("_id", false));
try{
while(cur.hasNext()){
DBObject doc = cur.next();
System.out.println(doc);
}
}finally{
cur.close();
}
}
}
Download the CODE from here.
No comments:
Post a Comment