Creating a complex JSON for in MongoDB.
Embedded/sub document example of MongoDB with JAVA.
Lets aim to add a JSON as shown below in mongoDB.
{
"Id" : 12345,
"Name" : "opqrst",
"Age" : 25,
"DateOfBirth" : ("1984-05-08"),
"Interests" : [
"Reading",
"Politics",
"Basketball"
],
"Address" : {
"addressDetail" : "flat no 103, A Wing, Ram Nagar",
"area" : "Rahatani",
"city" : "Pune",
"pincode" : 411027,
"state" : "MAHARASHTRA",
"country" : "INDIA"
}
}
public static void create(){
MongoClient mongoClient = null ;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "mongoDB" );
DBCollection documents = db.getCollection("Documents");
BasicDBObject document = new BasicDBObject();
document.put("Id", 12345);
document.put("Name", "opqrst");
document.put("Age", 25);
try {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("1984-05-08T8:30:00.000Z");
document.put("DateOfBirth", date);
} catch (ParseException e) {
e.printStackTrace();
}
String [] interests = {"Reading", "Politics", "Basketball"};
document.put("Interests", interests);
BasicDBObject address = new BasicDBObject();
address.put("addressDetail", "flat no 103, A Wing, Ram Nagar" );
address.put("area", "Rahatani");
address.put("city", "Pune");
address.put("pincode", 411027);
address.put("state", "MAHARASHTRA");
address.put("country", "INDIA");
document.put("Address", address);
documents.insert(document);
}
The output on the mongo shell is like :
> db.Documents.find().pretty();
{
"_id" : ObjectId("5296e4fe91093f95cc8b84e7"),
"Id" : 12345,
"Name" : "opqrst",
"Age" : 25,
"DateOfBirth" : ISODate("1984-05-08T03:00:00Z"),
"Interests" : [
"Reading",
"Politics",
"Basketball"
],
"Address" : {
"addressDetail" : "flat no 103, A Wing, Ram Nagar",
"area" : "Rahatani",
"city" : "Pune",
"pincode" : 411027,
"state" : "MAHARASHTRA",
"country" : "INDIA"
}
}
Creating more complex Structure using MongoDB and JAVA
Example of creating array of embedded/sub document.
The JSON structure is :
{
"Id" : 12345,
"Name" : "abcdefxyz",
"Age" : 25,
"DateOfBirth" : ("1984-05-08"),
"Interests" : [ "Reading",
"Politics", "Basketball"],
"address" : {
"addressDetail" : "flat no 333, Q Wing, Sita Nagar",
"area" : "Aundh",
"city" : "Pune",
"pincode" : 411017,
"state" : "MAHARASHTRA",
"country" : "INDIA"
},
"BooksRead" : [
{
"name" : "The Immortals of Meluha",
"authorName" : "Amish Tripathi",
"publishedBy" : "Westland Press"
},
{
"name" : "The Krishna Key",
"authorName" : "Ashwin Sanghi",
"publishedBy" : "Westland Ltd"
},
{
"name" : "Sita: An Illustrated Retelling of Ramayana",
"authorName" : "Devdutt Pattanaik",
"publishedBy" : "Mehata Publishing Ltd"
}
]
}
public static void create(){
MongoClient mongoClient = null ;
try {
mongoClient = new MongoClient( "localhost" , 27017 );
} catch (UnknownHostException e) {
e.printStackTrace();
}
DB db = mongoClient.getDB( "mongoDB" );
DBCollection documents = db.getCollection("Documents");
BasicDBObject document = new BasicDBObject();
document.put("Id", 12345);
document.put("Name", "abcdefxyz");
document.put("Age", 25);
document.put("DateOfBirth", new Date());
try {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse("1984-05- 08T8:30:00.000Z");
document.put("DateOfBirth", date);
} catch (ParseException e) {
e.printStackTrace();
}
String [] interests = {"Reading", "Politics", "Basketball"};
document.put("Interests", interests);
BasicDBObject address = new BasicDBObject();
address.put("addressDetail", "flat no 333, Q Wing, Sita Nagar" );
address.put("area", "Aundh");
address.put("city", "Pune");
address.put("pincode", 411017);
address.put("state", "MAHARASHTRA");
address.put("country", "INDIA");
document.put("address", address);
List<BasicDBObject> booksRead = new ArrayList<BasicDBObject>();
BasicDBObject book1 = new BasicDBObject();
book1.put("name", "The Immortals of Meluha");
book1.put("authorName", "Amish Tripathi");
book1.put("publishedBy", "Westland Press");
booksRead.add(book1);
BasicDBObject book2 = new BasicDBObject();
book2.put("name", "The Krishna Key");
book2.put("authorName", "Ashwin Sanghi");
book2.put("publishedBy", "Westland Ltd");
booksRead.add(book2);
BasicDBObject book3 = new BasicDBObject();
book3.put("name", "Sita: An Illustrated Retelling of Ramayana");
book3.put("authorName", "Devdutt Pattanaik");
book3.put("publishedBy", "Mehata Publishing Ltd");
booksRead.add(book3);
document.put("BooksRead", booksRead);
documents.insert(document);
}
The output on the mongo shell is like :
> db.Documents.find().pretty();
{
"_id" : ObjectId("5296e987910919e6768c9391"),
"Id" : 12345,
"Name" : "abcdefxyz",
"Age" : 25,
"DateOfBirth" : ISODate("1984-05-08T03:00:00Z"),
"Interests" : [ "Reading", "Politics","Basketball"],
"address" : {
"addressDetail" : "flat no 333, Q Wing, Sita Nagar",
"area" : "Aundh",
"city" : "Pune",
"pincode" : 411017,
"state" : "MAHARASHTRA",
"country" : "INDIA"
},
"BooksRead" : [
{
"name" : "The Immortals of Meluha",
"authorName" : "Amish Tripathi",
"publishedBy" : "Westland Press"
},
{
"name" : "The Krishna Key",
"authorName" : "Ashwin Sanghi",
"publishedBy" : "Westland Ltd"
},
{
"name" : "Sita: An Illustrated Retelling of Ramayana",
"authorName" : "Devdutt Pattanaik",
"publishedBy" : "Mehata Publishing Ltd"
}
]
}