Creating a blog post feature in my web application has been an interesting journey. Initially, I relied on mysql as the database to store the posts entered in the text area of the blog as an object in JavaScript. The process involved sending this object to the Java server side, where I would execute MySQL queries to retrieve the object in a result set and save it in the database. However, I have now decided to transition to using mongoDB for the same purpose. While I have grasped the basics from various tutorials, putting that knowledge to practice in my application has proven to be a bit challenging. I am eager to understand how the object from the JS will be passed within the loop and the correct approach to querying to save the object. Similarly, if there is a need to send an object from the server side to JS, I would like to know the best way to do so.
Snippet of My Server-Side Code:
public DB MongoConnection(Blog blog) throws UnknownHostException, MongoException{
Mongo m = new Mongo("localhost" , 27017); //mongo object
DB db = m.getDB("myblog");
System.out.println("Connected");
//creating a collection object, similar to a table in SQL
DBCollection items = db.getCollection("items");
System.out.println("Retrieved items collection");
//manipulating documents using BasicDbObject
BasicDBObject query = new BasicDBObject();
System.out.println("Mongo object created");
//Cursor, similar to ResultSet in SQL
DBCursor cursor = items.find();
System.out.println("Retrieved items");
while(cursor.hasNext()){
System.out.println(cursor.next());
}
While I have been able to understand the functionalities of establishing a mongo connection, working with documents, collections, and cursors, I am now seeking guidance on how to effectively save the object received from JS into mongoDB. Any recommendations or suggestions would be greatly appreciated.