Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

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");

    }/*from w w  w  .  ja  va2 s.c  om*/
}

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");

    }//ww  w .  j a va 2 s . c o m
}

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;/*from w w  w . j a  va2 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 ");

}

From source file:MenuProb.java

public static void getProbability() {

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

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

    for (Document doc : results) {

        List<String> conv = (List<String>) doc.get("Probability");

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

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

    }//from w  w w .  j  ava  2 s.c o  m
}

From source file:MenuDefAct.java

public static void getActions() {

    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("Actions");

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

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

    }/*from w w  w  . ja  v  a 2  s.c  om*/
}

From source file:BlogPostDAO.java

License:Apache License

public BlogPostDAO(final MongoDatabase blogDatabase) {
    postsCollection = blogDatabase.getCollection("posts");
}

From source file:MenuDefTyp.java

public static void getTypes() {

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

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

    for (Document doc : results) {
        // do something with each result

        List<String> conv = (List<String>) doc.get("Types");

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

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

    }//from w  w w.  j a v  a2 s. com
}

From source file:MenuDefPred.java

public static void getPreds() {

    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("Predicates");

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

        for (String s : convArr)
            PredTextArea.append(s + "\n");
    }/*from   w ww.  j a  va  2s . co m*/
}

From source file:Welcome.java

public void setListVal() {
    int i = 1;//  ww w  .j a  va  2s .co m
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase db = client.getDatabase("ExpenseManager");
    MongoCollection<Document> reg = db.getCollection("Registration");
    FindIterable<Document> find = reg.find(new Document("unm", unm));

    Document doc = find.first();

    DefaultListModel model;
    model = new DefaultListModel();
    while (i <= 5) {
        if (!doc.containsKey("Account" + i)) {
            i++;
        } else {
            while (doc.containsKey("Account" + i)) {
                String s = (String) doc.get("Account" + i);
                s = s.concat("              ");
                s = s.concat((String) doc.get("Bank" + i));
                s = s.concat("              ");
                s = s.concat((String) doc.get("Money" + i));
                model.addElement(s);
                i++;
            }
        }
    }
    jList1.setModel(model);
}

From source file:Welcome.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    /*MongoClient client = new MongoClient("localhost", 27017);
        DB db;//from   w ww  . ja  v a  2s.  c  om
        db = client.getDB("ExpenseManager");
        DBCollection reg = (DBCollection) db.getCollection("Registration");
        DBObject query = new BasicDBObject("unm", unm);
        DBObject update = new BasicDBObject();
                
        update.put("$unset", new BasicDBObject("userId",""));
    */
    int i = 1;
    int count = 1;
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase db = client.getDatabase("ExpenseManager");
    MongoCollection<Document> reg = db.getCollection("Registration");
    FindIterable<Document> find = reg.find(new Document("unm", unm));
    DBObject query = new BasicDBObject("unm", unm);
    BasicDBObject update = new BasicDBObject();
    Map<Integer, Integer> m = new HashMap<Integer, Integer>();
    String val[] = new String[6];
    val[0] = "";
    Document doc = find.first();
    while (i <= 5) {
        if (!doc.containsKey("Account" + i)) {
            i++;
        } else {
            while (doc.containsKey("Account" + i)) {
                m.put(count, i);
                val[count] = (String) (doc.get("Account" + i));
                i++;
                count++;
            }
        }
    }
    JComboBox jcb = new JComboBox(val);
    jcb.setEditable(true);
    int msg = JOptionPane.showConfirmDialog(this, "Do You Really Want to remove Your Account?");
    if (msg == 0) {
        JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove", JOptionPane.QUESTION_MESSAGE);

        while (jcb.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(this, "You Haven't chose any Account, Please Choose the Account");
            JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove",
                    JOptionPane.QUESTION_MESSAGE);
        }
        int index = jcb.getSelectedIndex();
        int value = m.get(index);
        String str1 = "Account" + value;
        String str2 = "Bank" + value;
        String str3 = "Money" + value;
        update.put("$unset", new BasicDBObject(str1, "").append(str2, "").append(str3, ""));
        UpdateResult updateOne = reg.updateOne(new Document("unm", unm), update);
        setListVal();

    }

    // TODO add your handling code here:
}