Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

In this page you can find the example usage for com.mongodb MongoClient MongoClient.

Prototype

public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:DeleteUser.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.setHeader("Access-Control-Allow-Origin", "*");
    try {/*from w ww .  j  a  v a 2 s.co  m*/
        out = response.getWriter();
        String id = request.getParameter("id");
        System.out.println("id received : " + id);
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your databases
        DB db = mongoClient.getDB("XHiring");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("users");
        System.out.println("Collection users selected successfully");
        //read example
        try {
            BasicDBObject query = new BasicDBObject("_id", new ObjectId(id));
            coll.findAndRemove(query);
            success = new JSONObject();
            success.put("message", "document deleted");
            out.println(success);
        } catch (NullPointerException ex) {
            error = new JSONObject();
            error.put("message", "No result found");
            out.println(error);
        }
        System.out.println("Document deleted successfully");
    } catch (Exception e) {
        exception = new JSONObject();
        try {
            exception.put("message", "exception");
            out.println(exception);
        } catch (JSONException ex) {
            Logger.getLogger(DeleteUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
    }

}

From source file:Dbconnection.java

public Connection getconnection() {

    Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
    mongoLogger.setLevel(Level.SEVERE);
    // To connect to mongodb server
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    MongoDatabase db = mongoClient.getDatabase("mydb");
    System.out.println("Connect to database successfully");
    //boolean auth = db.authenticate("","");
    //System.out.println("Authentication: "+auth);
    // MongoCollection<Document> coll = db.getCollection("yelp");

    System.out.println("Collection yelp selected successfully");

    /*FindIterable<Document> cursor = coll.find();
                 //from ww w  .  j av  a 2s .co  m
         cursor.forEach(new Block<Document>() {
         @Override
         public void apply(final Document document) {
             System.out.println(document);
         }
     });*/
    //            BasicDBObject clause1 = new BasicDBObject("yelping_since", "2012-02");
    //            //clause1.put("yelping_since", "2012-02");
    //            
    //            BasicDBObject clause2 = new BasicDBObject("review_count",new BasicDBObject("$gt",500));
    //            BasicDBObject clause3 = new BasicDBObject("fans",new BasicDBObject("$gt",15)); 
    //            BasicDBObject clause4 = new BasicDBObject("average_stars",new BasicDBObject("$gt",3)); 
    //            //BasicDBObject cl5 = new BasicDBObject("$exist",true);
    //            //BasicDBObject cl6 = new BasicDBObject("$where","this.friends.length>49");
    //            //String cl = cl5.toString();
    //            //BasicDBObject c = new BasicDBObject(cl,cl6);
    //            //BasicDBObject f = new BasicDBObject("friends",new BasicDBObject("$exist",true));
    //            BasicDBObject clause5 = new BasicDBObject("friends.50",new BasicDBObject("$exists",true));
    //            //BasicDBObject cl5 = new BasicDBObject("$where","this.friends.length>3");
    //            
    //            /*BasicDBList fl = new BasicDBList();
    //            fl.add(f);
    //            fl.add(cl6);*/
    //            
    //            //clause2.put("review_count",gtquery);
    //            BasicDBList or = new BasicDBList();
    //            
    //            or.add(clause1);
    //            or.add(clause2);
    //            or.add(clause3);
    //            or.add(clause4);
    //            or.add(clause5);
    //            //or.add(fl);
    //            
    //            //fields.put("name",1);
    //            //fields.put("average_stars",1);
    //            //fields.put("fans",1);
    //            BasicDBObject query = new BasicDBObject("$and", or);
    //            
    //            
    //            FindIterable<Document> cursor = coll.find(query);
    //            System.out.println(query);
    //            cursor.forEach(new Block<Document>() {
    //                @Override
    //                public void apply(final Document document) {
    //                                System.out.println(document.get("user_id"));
    //                                System.out.println(document.get("name"));
    //                                System.out.println(document.get("fans"));
    //                                System.out.println(document.get("average_stars"));
    //                                System.out.println(document.get("friends"));
    //                                
    //                }
    //            });
    //
    //        

    mongoClient.close();
    System.out.println("Connection successfully closed");

    return null;

}

From source file:Vehicle_exit.java

public void fetch() {
    try {//  w w  w . jav a  2s. co m
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collectionx = db.getCollection("entry_info");

        if (veh_count == 2) {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '2') {

                    vehno_combo.addItem(objx.get("vehno").toString());

                }
                System.out.println("hello");

            }
        } else if (veh_count == 4) {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '4') {

                    vehno_combo.addItem(objx.get("vehno").toString());
                    //vehno = vehno_combo.getSelectedItem().toString();
                }
                System.out.println("hello");

            }
        }

        else {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '6') {

                    vehno_combo.addItem(objx.get("vehno").toString());
                    //vehno = vehno_combo.getSelectedItem().toString();
                }
                System.out.println("hello");

            }
        }
    } catch (Exception e) {
    }
}

From source file:Vehicle_exit.java

private void Exit_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Exit_buttonActionPerformed
    // TODO add your handling code here:

    try {/*from w  w w  . j a v a2  s . com*/
        vehno = vehno_combo.getSelectedItem().toString();
        vehno_copy = vehno;
        System.out.println("in button number niald" + vehno);

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("entry_info");

        BasicDBObject query_time = new BasicDBObject();
        BasicDBObject field_time = new BasicDBObject();
        field_time.put("intime", 1);
        DBCursor cursor = collection.find(query_time, field_time);

        BasicDBObject query_token = new BasicDBObject();
        BasicDBObject field_token = new BasicDBObject();
        field_time.put("vehno", 1);
        DBCursor cursor_token = collection.find(query_token, field_token);

        while (cursor.hasNext()) {
            BasicDBObject obj_time = (BasicDBObject) cursor.next();
            in_time_mills = Long.parseLong(obj_time.getString("intime"));

            BasicDBObject obj_token = (BasicDBObject) cursor_token.next();
            //db_token = obj_token.getInt("_id");
            vehno_db = obj_token.getString("vehno");
            System.out.println(vehno_db);
            System.out.println("hello");

            System.out.println("hey ayu");

            DBCollection collection1 = db.getCollection("vehicle_count");
            DBCollection collection2 = db.getCollection("statistics");
            vehno = vehno_copy;
            if (veh_count == 2) {
                //vehno = vehno + "W2";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_bike;
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_2", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_bike = obj.getInt("wheeler_2");

                    System.out.println(count_bike);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_2", count_bike);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 20 + (time_diff) / 2;
                    else
                        charges = 20;

                    //updating session...

                    int income_bike;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("2wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_bike = obj2.getInt("2wheeler_income");
                    System.out.println(income_bike);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("2wheeler_income", income_bike);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("2wheeler_income", income_bike + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;
                }
            }

            else if (veh_count == 4) {

                //vehno = vehno + "W4";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_car;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_4", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_car = obj.getInt("wheeler_4");
                    System.out.println(count_car);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_4", count_car);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_4", count_car - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...2");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 40 + (time_diff) * 3 / 2;
                    else
                        charges = 40;

                    //Session update....

                    int income_car;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("4wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_car = obj2.getInt("4wheeler_income");
                    System.out.println(income_car);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("4wheeler_income", income_car);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("4wheeler_income", income_car + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checking...2");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            } else {

                //vehno = vehno + "W6";
                if (vehno.equals(vehno_db)) {

                    flag = 1;

                    BasicDBObject deleteObject = new BasicDBObject();
                    //deleteObject.put("_id",Token);
                    deleteObject.put("vehno", vehno);
                    DBCursor findQuery = collection.find(deleteObject);
                    collection.remove(deleteObject);

                    int count_other;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("other", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_other = obj.getInt("other");

                    System.out.println(count_other);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("other", count_other);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("other", count_other - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...3");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 60 + (time_diff);
                    else
                        charges = 60;

                    //sessiuon update....

                    int income_other;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("other_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_other = obj2.getInt("other_income");
                    System.out.println(income_other);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("other_income", income_other);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("other_income", income_other + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...3");
                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            }
            //}
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "Invalid Vehicle Number or Type");
            setVisible(false);
            new Entry_OR_Exit().setVisible(true);
        }
        /*
        BasicDBObject searchUpdate = new BasicDBObject();
        searchUpdate.append("wheeler_2", count_bike);
                
        BasicDBObject UpdateObject = new BasicDBObject();
        UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike + 1));
                
        collection.update(searchUpdate, UpdateObject);
                
        */
        //}
    } catch (Exception e) {

    }

}

From source file:AllUser.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        al = new ArrayList<String>();
        // To connect to mongodb server
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // Now connect to your databases
        DB db = mongoClient.getDB("XHiring");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("users");
        System.out.println("Collection mycol selected successfully");

        DBCursor cursor = coll.find();/*from   w  ww.  j a v a  2  s.co  m*/
        int i = 1;
        try {
            while (cursor.hasNext()) {

                al.add(cursor.next().toString());
                i++;
            }
        } finally {
            System.out.println("arra: " + al);
            Genson obj = new Genson();
            out.println(obj.serialize(al));
        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:MenuDefCats.java

public static void getCats() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Cats");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            CatsTextArea.append(s + "\n");

    }/*w w  w  .  j  av a  2 s.  c o m*/
}

From source file:MenuDefObj.java

public static void getObjects() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Objects");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            ObjTextArea.append(s + "\n");

    }/*from   w ww.  ja v  a 2  s .  c  o m*/
}

From source file:MainFrame.java

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    // TODO add your handling code here:
    conn = new MongoClient("localhost", 27017);
    db = conn.getDB("Project");
    Login login = new Login(conn, db, frame);
    login.setSize(this.getSize());
    System.out.println("here");
    this.add(login);

}

From source file:FollowingCnt.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w w  . ja va  2  s.c o m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        BasicDBObject query = new BasicDBObject("username", username);
        DBCursor curssc = User.find(query);
        ArrayList<String> e = new ArrayList();
        e = (ArrayList<String>) curssc.next().get("following_username");
        out.println(e.size());
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:StartScreen.java

private void insertMongo() throws Exception {
    // *****This is to connect to database**//

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");

    MongoCollection<Document> elexirCollection = db.getCollection("test");

    // *********This is to connect to the database***********//

    //To clear out existing files from mongo
    db.getCollection("test").deleteMany(new Document());

    // *******This is to read the file into program*********//
    //String fileDirectory = chooser.getCurrentDirectory() + "";

    FileReader file = new FileReader(pathField.getText());
    BufferedReader reader = new BufferedReader(file);

    String line = reader.readLine();

    // **********This is to read the text file into program***************//

    // Creating the Array List to store types of variables
    List<String> Types = new ArrayList<String>();
    List<String> Objects = new ArrayList<String>();
    List<String> Predicates = new ArrayList<String>();
    List<String> Cats = new ArrayList<String>();
    List<String> Category = new ArrayList<String>();
    List<String> Action = new ArrayList<String>();

    List<Double> DerivProb = new ArrayList<Double>();
    List<Double> RootProb = new ArrayList<Double>();
    List<String> InitialState = new ArrayList<String>();
    List<String> FinalState = new ArrayList<String>();
    List<String> Roots = new ArrayList<String>();

    List<String> Probability = new ArrayList<String>();

    ArrayList<Document> Doc = new ArrayList<Document>();

    // initialize all types of definitions
    String type = null;/* ww w  .java  2 s  . c o m*/
    String object = null;
    String predicate = null;
    String cat = null;
    String category = null;
    String action = null;
    String derivProb = null;
    String rootProb = null;
    String initialState = null;
    String finalState = null;
    String roots = null;
    String probability = null;

    Document Exp = new Document();
    Document definitions = new Document();
    Document stats = new Document();

    // Read type Definitions
    while (line != null) {

        if (line.contains("Defined Type: ")) {
            int startingIndexOfType;
            String types = "Defined Type: ";
            startingIndexOfType = line.indexOf("Defined Type: ");
            int endingIndexOfType = line.indexOf(".");
            type = line.substring(startingIndexOfType + types.length(), endingIndexOfType);
            // putting the piece of string into the new string
            Types.add(type);

        }

        // Read object definitions.
        else if (line.contains("Defined Object: ")) {
            int startingIndexOfObj; // this is to split each word from its
            // spaces and print word by word
            String objects = "Defined Object: ";
            startingIndexOfObj = line.indexOf("Defined Object: ");
            int endingIndexOfObj = line.indexOf(".");
            object = line.substring(startingIndexOfObj + objects.length(), endingIndexOfObj);
            // putting the piece of string into the new string
            Objects.add(object);
        }

        // Read predicate definitions.
        else if (line.contains("Defined predicate:")) {
            String predicates = "Defined predicate:";
            int startingIndexOfPred; // this is to split each word from its
            // spaces and print word by word
            startingIndexOfPred = line.indexOf("Defined predicate:");
            int endingIndexOfPred = line.indexOf(".");
            predicate = line.substring(startingIndexOfPred + predicates.length(), endingIndexOfPred);
            // putting the piece of string into the new string
            Predicates.add(predicate);
        }

        // Defined Cat-Definition
        else if (line.contains("Defined: Cat-Definition: ")) {
            int startingIndexOfCat; // this is to split each word from its
            // spaces and print word by word
            String catDef = "Defined: Cat-Definition: ";
            startingIndexOfCat = line.indexOf("Defined: Cat-Definition: ");
            int endingIndexOfCat = line.indexOf(".", startingIndexOfCat);
            cat = line.substring(startingIndexOfCat + catDef.length(), endingIndexOfCat);
            // putting the piece of string into the new string
            Cats.add(cat);
        }

        // Defined Action Definitions

        else if (line.contains("Defined: category: ")) {
            String categ = "Defined: category: ";
            int startingIndexOfCategory = line.indexOf("Defined: category: ");
            int endingIndexOfCategory = line.indexOf(";", startingIndexOfCategory);
            category = line.substring(startingIndexOfCategory + categ.length(), endingIndexOfCategory);
            Category.add(category);

        }

        else if (line.contains("Defined: ")) {
            // this is to split each word from its
            // spaces and print word by word
            String defined = "Defined: ";
            int startingIndexOfAct = line.indexOf("Defined: ");
            int endingIndexOfAct = line.indexOf(".", startingIndexOfAct);
            action = line.substring(startingIndexOfAct + defined.length(), endingIndexOfAct);
            // putting the piece of string into the new string
            Action.add(action);

        }
        line = reader.readLine();
        definitions = new Document();
        if (line.startsWith("Read goals for query."))
            break;

    }

    definitions.append("Types", Types).append("Objects", Objects).append("Predicates", Predicates)
            .append("Cats", Cats).append("Category", Category).append("Actions", Action);

    elexirCollection.insertOne(definitions);

    while (line != null) {

        DerivProb = new ArrayList<Double>();
        RootProb = new ArrayList<Double>(); // To clear out the array so that
        InitialState = new ArrayList<String>(); // the document does not repeat with
        FinalState = new ArrayList<String>(); // roots from previous explanations
        Roots = new ArrayList<String>();

        if (line.startsWith("[Exp:")) {
            String dp = "derivProb :";
            int startingIndexOfDP;
            startingIndexOfDP = line.indexOf("derivProb: ");
            int endingIndexOfDP = line.indexOf(" root", startingIndexOfDP);
            derivProb = line.substring(startingIndexOfDP + dp.length(), endingIndexOfDP);
            DerivProb.add(Double.parseDouble(derivProb));

            String rp = "rootProb :";
            int startingIndexOfRP;
            startingIndexOfRP = line.indexOf("rootProb: ");
            int endingIndexOfRP = line.indexOf(" Initial", startingIndexOfRP);
            rootProb = line.substring(startingIndexOfRP + rp.length(), endingIndexOfRP);
            RootProb.add(Double.parseDouble(rootProb));

            String is = "Initial State:[ ";
            int startingIndexOfIS;
            startingIndexOfIS = line.indexOf("Initial State:[ ");
            int endingIndexOfIS = line.indexOf(" ]", startingIndexOfIS);
            initialState = line.substring(startingIndexOfIS + is.length(), endingIndexOfIS);
            InitialState.add(initialState);

            String fs = "Final State:[ ";
            int startingIndexOfFS;
            startingIndexOfFS = line.indexOf("Final State:[ ");
            int endingIndexOfFS = line.indexOf(" ]", startingIndexOfFS);
            finalState = line.substring(startingIndexOfFS + fs.length(), endingIndexOfFS); // capture last 
            FinalState.add(finalState);

            String rootStr = "root:[";
            for (int x = line.indexOf(rootStr); x > -1; x = line.indexOf(rootStr, ++x)) {
                int endingIndexOfRoot = line.indexOf("]", x);
                roots = line.substring(x + rootStr.length(), endingIndexOfRoot);

                Roots.add(roots);

            }

            Exp = new Document();
            Exp.append("derivProb", Arrays.asList(derivProb)).append("rootProb", Arrays.asList(rootProb))
                    .append("initialState", Arrays.asList(initialState))
                    .append("finalState", Arrays.asList(finalState)).append("Roots", Roots);

            Doc.add(Exp);
            elexirCollection.insertOne(Exp);

        }

        else if (!line.startsWith("[Exp:")) {
            //System.out.println("At first if else");
            // System.out.println(line);
            if (line.contains("Probabilites:")) {
                //System.out.println("At line equals prob");
                while (!line.contains("*** Done with problem. ***")) {
                    //System.out.println("Reached while loop");
                    //read each line and save here

                    probability += line + "\n";

                    //System.out.println(probability);

                    line = reader.readLine();

                }

                Probability.add(probability);
                stats.append("Probability", Probability);
                elexirCollection.insertOne(stats);
            }

        }

        line = reader.readLine();
    } // end while    

    FindIterable<Document> iter = elexirCollection.find();
    System.out.println("Your Documents have been stored into mongoDB ");

}