Searching...
Wednesday, 19 September 2012

Java Mongodb BasicDBObjectBuilder Example

00:39

BasicDBObjectBuilder

BasicDBObjectBuilder is a utility class for building java complex object for mongodb persistance. Please find the below example.

There are three ways can create BasicDBObjectBuilder object

1. Creates an empty object and adding values 
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("name", "vinod")
.add("address", "bangalore");

2. Creates object with given key/value
BasicDBObjectBuilder documentBuilder1 = BasicDBObjectBuilder.start("name","vinod");

3. Creates object with Map
Map<String, String> map=newHashMap<String, String>();
map.put("name", "vinod");
map.put("address", "bangalore");
BasicDBObjectBuilder documentBuilder2 = BasicDBObjectBuilder.start(map);

Full Example

package com.vinod.mongo.crud;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoBasicDBObjectBuilderExample {

public static void main(String[] args) {
try {
System.out.println("Mongodb Basicdb object example...");
// Creating mongoinstance
Mongo mongo = new Mongo("localhost", 27017);
// Creating database instance
DB db = mongo.getDB("mydb");

// Creating collection object
DBCollection collection = db.getCollection("Employee");
// Creating BasicDBObjectBuilder object without arguments
BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start()
.add("name", "vinod").add("address", "bangalore");
// get the dbobject from builder and Inserting document
collection.insert(documentBuilder.get());
// Creating BasicDBObjectBuilder object with arguments
BasicDBObjectBuilder documentBuilder1 = BasicDBObjectBuilder
.start("name", "vinod");
collection.insert(documentBuilder1.get());
// Creating BasicDBObjectBuilder object with map arguments
Map<String, String> map = new HashMap<String, String>();
map.put("name", "vinod");
map.put("address", "bangalore");
BasicDBObjectBuilder documentBuilder2 = BasicDBObjectBuilder
.start(map);
collection.insert(documentBuilder2.get());
// Retrieving collection details
DBCursor cursorDoc = collection.find();
while (cursorDoc.hasNext()) {
System.out.println(" details " + cursorDoc.next());
}
} catch (MongoException e) {
e.printStackTrace();
}
}

}

Output

details { "_id" : { "$oid" : "56897261f5d3c814db20c5de"} , "name" : "vinod" , "address" : "Bangalore"}
details { "_id" : { "$oid" : "568972aef5d3c814ddb7a3d6"} , "name" : "vinod" , "address" : "Bangalore"}
details { "_id" : { "$oid" : "56897d55f5d3c81553b10d46"} , "name" : "vinod" , "address" : "Bangalore"}
details { "_id" : { "$oid" : "568f73cdf5d3c83ecf8a8015"} , "name" : "vinod" , "address" : "bangalore"}
details { "_id" : { "$oid" : "568f73cdf5d3c83ecf8a8016"} , "name" : "vinod"}
details { "_id" : { "$oid" : "568f73cdf5d3c83ecf8a8017"} , "address" : "bangalore" , "name" : "vinod"}

Done !!

Download example

https://github.com/kkvinodkumaran/myrepository/tree/master/mymongoexamples

0 comments: