1

I want to make a blog post in my web application. Initially I was using mysql as DB. In which i will get the post entered in the text area of blog as an object in JS and send that object to java server side. There I will write mysql query and get the object in resultset and save in database. But now i want to use mongoDB for the same. I am able to understand the basic stuff through many tutorials which I learnt. But I am unable to implement that in my application. I want to know how the object that comes from the JS will be sent inside the loop and how I should query to save the object also similarly if I need to send a object from server side to JS. How should i do.?

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");
    //making a collection object which is table when compared to sql
    DBCollection items = db.getCollection("items"); 
    System.out.println("items got");

   //to work with document we need basicDbObject        
    BasicDBObject query = new BasicDBObject();
    System.out.println("Created mongoObject");

      //Cursor, which is like rs in sql
    DBCursor cursor = items.find();
    System.out.println("items got");
     while(cursor.hasNext()){
        System.out.println(cursor.next());
    } 

In the above code i understood everything such as how a mongo connection, documents, collections and cursor's work. Now how should i save the value coming as an object from JS and save in mongoDB. Any Suggestions Please?

2
  • mongodb.org/display/DOCS/Java+Tutorial Commented Nov 30, 2010 at 19:36
  • @williams thanks I went through this tutorial before. my question is I am not able to compare this with mysql and implement it. i donot know how to implement. If i am vague sorry for that, just i wanted to explain my situation.I am a newbie trying to go forward. thanks. Commented Nov 30, 2010 at 19:53

1 Answer 1

1

Use method save of DBCollection class something like that:

while(cursor.hasNext()){
    DBObject doc = cursor.next();

    doc.put("name", "Leo-vin");

    items.save(doc);
}

method cursor.next() returns object of type DBObject. It is your BSONObject.

Update:

to modify document (BSON) use method put of class BSONObject

Sign up to request clarification or add additional context in comments.

5 Comments

@Edward thanks. my object blog comes from js. is that object is obj in your answer or what? Please i am confused chnage your obj means How I should change? can you say in words.I wish to understand things and do.so pl can you explain.
MongoDB does not store records like relative dbs. It stores documents;)
@Leo-vin, i changed obj to doc. It will be more understandable to read;
@Leo-vin, i think that as first step you can try to convert your tables into BSON docs. It is not very "true" conversion, but it can work! So, one table's record -> one bson document. Sorry! I can't help in such conversion because i do not have experience with relational databases;)
@edward thanks I am trying it. Thanks for your help. its very useful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.