Example usage for com.mongodb.client MongoDatabase listCollectionNames

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

Introduction

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

Prototype

MongoIterable<String> listCollectionNames();

Source Link

Document

Gets the names of all the collections in this database.

Usage

From source file:org.flywaydb.core.internal.dbsupport.mongo.MongoDatabaseUtil.java

License:Apache License

/**
 * Checks whether a Mongo collection exists.
 *
 * @param client a MongoClient for connecting to the database.
 * @param dbName the database name./*from   w w w.  j a va 2  s  .co m*/
 * @param name the collection name.
 * @return {@code true} if it does, {@code false} if not.
 */
public static boolean hasCollection(MongoClient client, String dbName, String name) {
    try {
        MongoDatabase mongoDb = client.getDatabase(dbName);
        MongoIterable<String> collectionNameIterator = mongoDb.listCollectionNames();
        for (String collectionName : collectionNameIterator) {
            if (collectionName.equals(name))
                return true;
        }
        return false;
    } catch (MongoException e) {
        throw new FlywayException("Unable to check whether collection " + name + " exists", e);
    }
}

From source file:org.kiaan.Main.java

/**
public static class view_main extends JFrame {            
        //  w  w w  .  jav  a  2  s . c  o  m
public view_main() throws HeadlessException, IOException {            
    //
            
    addKeyListener(getInputKey());
    //            
    getContentPane();
    setTitle(" ?  ");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setScreenSize();
//            add BorderLayout
    BorderLayout Layout = new BorderLayout();
    Layout.setHgap(3);Layout.setVgap(3);
    setLayout(Layout);
//        add Top
    JPanel pnlMainTop = new JPanel();
    pnlMainTop.setBackground(Color.LIGHT_GRAY);
    add(pnlMainTop, BorderLayout.PAGE_START);
    JButton btn = new JButton("change");
    pnlMainTop.add(btn);
//        add left
    JPanel pnlMainLeft = new JPanel();
    pnlMainLeft.setBackground(Color.LIGHT_GRAY);
    add(pnlMainLeft, BorderLayout.LINE_START);
//        add right
    JPanel pnlMainRight = new JPanel();
    pnlMainRight.setBackground(Color.LIGHT_GRAY);
    add(pnlMainRight, BorderLayout.LINE_END);
//        add Center
    JPanel pnlMainCenter = new JPanel();
    pnlMainCenter.setBackground(Color.GRAY);
    add(pnlMainCenter, BorderLayout.CENTER);
            
    CardLayout cardLayoutMain = new CardLayout();
    pnlMainCenter.setLayout(cardLayoutMain);
    JPanel subPanelMain = new JPanel(); 
    subPanelMain.setBackground(Color.white);
            
    myToggleButton tb_main = new myToggleButton("",false);            
    subPanelMain.add(tb_main);
            
//            JPanel subPanel2 = new JPanel(); subPanel2.setBackground(Color.blue);
    pnlMainCenter.add(subPanelMain, "1");
//            pnlMainCenter.add(subPanel2, "2");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
//                    cardLayoutMain.show(pnlMainCenter, "2");
            cardLayoutMain.next(pnlMainCenter);
        }
    });
        
            
//        add Bottom
    JPanel pnlMainBottom = new JPanel();
    pnlMainBottom.setBackground(Color.LIGHT_GRAY);
    add(pnlMainBottom, BorderLayout.PAGE_END);
            
            
            
//            JTable tbl = new JTable();
//            add(tbl, BorderLayout.CENTER);
            
            
            
        
}                            
        
private void setScreenSize() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(screenSize.width, screenSize.height);
}
}
**/

public static void db() {
    try {
        //connect to db
        MongoClient MongoClient = new MongoClient("localhost", 27017);
        MongoIterable<String> databaseNames = MongoClient.listDatabaseNames();
        //            MongoDatabase kiaanDB = MongoClient.getDatabase("kiaan");            
        //check exists kiaan db
        Boolean blnDBExists = false;
        for (String DBName : databaseNames) {
            if (DBName.equals("kiaan")) {
                blnDBExists = true;
                System.out.println("kiaan database is exists.");
                break;
            }
        }
        if (blnDBExists == false)
            System.out.println("create kiaan database...");
        MongoDatabase mydb = MongoClient.getDatabase("kiaan");
        System.out.println("Connect to database successfully");
        //Check exist user collection
        Boolean blnCollectionName = false;
        for (String collectionName : mydb.listCollectionNames()) {
            if (collectionName.equals("user")) {
                blnCollectionName = true;
                break;
            }
        }
        if (!blnCollectionName) {
            //create users collection
            ObjectId user_id = new ObjectId();
            Document doc = new Document().append("name", "")
                    //.append("credit_limit", 0)
                    //                        .append("status", true)
                    .append("comment", " ? ( ?)")
                    .append("creator_id", user_id).append("users",
                            new Document("_id", user_id).append("id", 1)
                                    .append("first_name", " ")
                                    .append("last_name", "( ? )")
                                    .append("user_name", "admin").append("password", "1602")
                                    //.append("status", false)
                                    .append("creator_id", user_id));
            mydb.getCollection("user").insertOne(doc);
            System.out.println("user collection created...");
            doc.clear();
            //
            BasicDBList dbl = new BasicDBList();
            ObjectId branch_id = new ObjectId();
            dbl.add(new Document("_id", branch_id).append("id", 8561).append("name", " ")
                    .append("city", "").append("creator_id", user_id));
            dbl.add(new Document("_id", new ObjectId()).append("id", 8576).append("name", "")
                    .append("city", "").append("creator_id", user_id));
            ObjectId bank_id = new ObjectId();
            doc = new Document("_id", bank_id).append("name", "")
                    //                        .append("logo", "")
                    .append("creator_id", user_id).append("branches", dbl);
            mydb.getCollection("bank").insertOne(doc);

            dbl = new BasicDBList();
            dbl.add(new Document("_id", new ObjectId()).append("id", 3238).append("name", " ")
                    .append("city", "").append("creator_id", user_id));
            doc = new Document().append("name", "")
                    //                        .append("logo", "")
                    .append("creator_id", user_id).append("branches", dbl);
            mydb.getCollection("bank").insertOne(doc);
            //
            doc = new Document().append("name", "").append("creator_id", user_id).append("branches",
                    new BasicDBList());
            mydb.getCollection("bank").insertOne(doc);
            //add doc to array
            //                DBObject listItem = new BasicDBObject("branches", new BasicDBObject("_id", new ObjectId())
            //                        .append("branch_id",8576)
            //                        .append("name","")
            //                        .append("city","")
            //                );
            //                DBObject findQuery = new BasicDBObject("name", "");
            //                DBObject updateQuery = new BasicDBObject("$push", listItem);
            //                mydb.getCollection("banks").update(findQuery, updateQuery);                                
            //
            System.out.println("bank collection created...");

            //add person
            doc.clear();
            dbl.clear();
            ObjectId person_id1 = new ObjectId();
            ObjectId person_id2 = new ObjectId();
            dbl.add(new Document("_id", person_id1).append("id", 1).append("type", "")
                    .append("first_name", "").append("last_name", "")
                    .append("comment", "     ")
                    .append("creator_id", user_id));

            BasicDBList dbl_tel = new BasicDBList();
            BasicDBList dbl_bankAcc = new BasicDBList();
            dbl_tel.add(new Document("_id", new ObjectId()).append("type", "mobile")
                    .append("number", "09151213139").append("default", true));
            dbl_tel.add(new Document("_id", new ObjectId()).append("type", "mobile").append("number",
                    "09151112233"));
            dbl_tel.add(new Document("_id", new ObjectId()).append("type", "work")
                    .append("number", "05133661313").append("default", true));
            dbl_bankAcc.add(new Document("_id", new ObjectId()).append("type", "bank_acc")
                    .append("bank_id", bank_id).append("number", "4218504285")
                    .append("comment", "  ").append("default", true));
            dbl_bankAcc.add(new Document("_id", new ObjectId()).append("type", "sheba")
                    .append("bank_id", bank_id).append("number", "600120020000004218504285"));
            dbl.add(new Document("_id", person_id2).append("id", 2).append("type", "")
                    .append("first_name", "").append("last_name", "")
                    .append("gender", true).append("credit_limit", 10000000)
                    .append("address",
                            "  -    ?   -  2/716")
                    .append("creator_id", user_id).append("tel", dbl_tel).append("bank_account", dbl_bankAcc));
            doc = new Document("name", "").append("creator_id", user_id).append("persons", dbl);
            mydb.getCollection("person").insertOne(doc);

            mydb.getCollection("person").insertOne(new Document("name", "")
                    .append("creator_id", user_id).append("persons", new BasicDBList()));
            System.out.println("person collection created...");
            //
            doc = new Document("id", 1).append("account_no", "0205575259006")
                    .append("account_holder", "? ").append("bank_id", bank_id)
                    .append("branch_id", branch_id).append("type", 0).append("comment", " ")
                    .append("creator_id", user_id);
            mydb.getCollection("bank_account").insertOne(doc);
            doc = new Document("id", 2).append("account_no", "0207723518008")
                    .append("account_holder", "? ").append("bank_id", bank_id)
                    .append("branch_id", branch_id).append("type", 1).append("creator_id", user_id);
            mydb.getCollection("bank_account").insertOne(doc);

            System.out.println("bank_account collection created...");
            //add units
            ObjectId unit_id = new ObjectId();
            doc = new Document("_id", unit_id).append("name", "").append("creator_id", user_id);
            mydb.getCollection("unit").insertOne(doc);
            System.out.println("Unit collection created...");
            //add products
            dbl = new BasicDBList();
            ObjectId product_id = new ObjectId();
            dbl.add(new Document("_id", product_id).append("id", 1).append("name", "")
                    .append("unit_id", unit_id).append("min_stock", 1000).append("default_price", 8300)
                    .append("comment", " ").append("creator_id", user_id));
            dbl.add(new Document("_id", new ObjectId()).append("id", 2).append("name", "")
                    .append("unit_id", unit_id).append("min_stock", 10).append("default_price", 80000)
                    .append("comment", "  ").append("creator_id", user_id));
            doc = new Document("name", "").append("comment", "  ")
                    .append("creator_id", user_id).append("products", dbl);
            mydb.getCollection("product").insertOne(doc);
            //
            dbl.clear();
            dbl.add(new Document("_id", new ObjectId()).append("id", 3).append("name", "")
                    .append("unit_id", unit_id).append("min_stock", 100).append("default_price", 2500)
                    .append("comment", "   ").append("creator_id", user_id));
            doc = new Document("name", "").append("comment", "   ")
                    .append("creator_id", user_id).append("products", dbl);
            mydb.getCollection("product").insertOne(doc);
            //
            System.out.println("product Document created...");

            //add buy
            doc.clear();
            dbl.clear();
            dbl.add(new Document("_id", new ObjectId()).append("product_id", product_id).append("value", 100)
                    .append("price", 9250).append("comment", "   "));
            dbl.add(new Document("_id", new ObjectId()).append("product_id", product_id).append("value", 350)
                    .append("price", 9350).append("comment", "   "));
            doc = new Document("_id", new ObjectId()).append("type", "buy").append("id", 1)
                    .append("person_id", person_id1).append("by", "? ").append("discount", -1500)
                    //                        .append("increase", 0)
                    .append("tax", 4000)
                    //                        .append("fare", 0)
                    .append("comment", "  ?").append("creator_id", user_id)
                    .append("items", dbl);
            mydb.getCollection("buy").insertOne(doc);
            //                doc.clear(); dbl.clear();
            //                dbl.add(new BasicDBObject("_id",new ObjectId())
            //                        .append("product_id", 1)
            //                        .append("value", 1000)
            //                        .append("price", 9300)                        
            //                );
            //                doc = new BasicDBObject("num", 2)
            //                        .append("date", new Date())                        
            //                        .append("person_id", person_id2)
            //                        .append("by", "? ")
            //                        .append("discount", 0)
            //                        .append("increase", 0)
            //                        .append("user_id", user_id)
            //                        .append("items", dbl);
            //                mydb.getCollection("buy").insert(doc);         
            System.out.println("buy Document created...");

        }

        //            System.out.println("Collection created successfully");
        //            BasicDBObject doc = new BasicDBObject("name","mongoDB");
        //            coll.insert(doc);
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:org.netbeans.modules.mongodb.ui.explorer.CollectionNodesFactory.java

License:Open Source License

@Override
protected boolean createKeys(final List<CollectionInfo> list) {
    MongoConnection connection = lookup.lookup(MongoConnection.class);
    DbInfo info = lookup.lookup(DbInfo.class);
    try {//from  w  w  w  . j  a  v a 2 s. co  m
        final MongoDatabase db = connection.getClient().getDatabase(info.getDbName());
        for (String name : db.listCollectionNames()) {
            list.add(new CollectionInfo(name, lookup));
        }
    } catch (MongoSocketException ex) {
        lookup.lookup(MongoConnection.class).disconnect();
    }
    Collections.sort(list);
    return true;
}

From source file:org.netbeans.modules.mongodb.ui.wizards.ExportVisualPanel1.java

License:Open Source License

public ExportVisualPanel1(MongoDatabase db) {
    initComponents();/*from www .  j  a  va2 s .  c o  m*/
    for (String collection : db.listCollectionNames()) {
        collectionComboBox.addItem(collection);
    }
    collectionComboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            changeSupport.fireChange();
        }
    });
}

From source file:org.netbeans.modules.mongodb.ui.wizards.ImportVisualPanel1.java

License:Open Source License

/**
 * Creates new form ImportVisualPanel1/*from w  ww.  j  a  v a  2s .c o m*/
 */
public ImportVisualPanel1(MongoDatabase db) {
    initComponents();
    collectionEditor = (JTextComponent) collectionComboBox.getEditor().getEditorComponent();
    dropCheckBox.setVisible(false);
    final File home = new File(System.getProperty("user.home"));
    fileChooser = new FileChooserBuilder("import-export-filechooser").setTitle("Import documents")
            .setDefaultWorkingDirectory(home).setApproveText("Import").setFileFilter(new JsonFileFilter())
            .setFilesOnly(true).createFileChooser();
    for (String collection : db.listCollectionNames()) {
        collectionComboBox.addItem(collection);
    }
    for (Charset charset : Charset.availableCharsets().values()) {
        encodingComboBox.addItem(charset);
    }
    final DocumentListener documentListener = new DocumentListener() {

        @Override
        public void insertUpdate(DocumentEvent e) {
            changeSupport.fireChange();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            changeSupport.fireChange();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            changeSupport.fireChange();
        }

    };
    fileField.getDocument().addDocumentListener(documentListener);
    collectionEditor.getDocument().addDocumentListener(documentListener);
}

From source file:org.piotr.apollo.mongo.migration.AnswerMigration.java

public AnswerMigration() {

    System.out.println("Answer migration : start");

    Boolean collExist = false;//from w  w w. ja  v  a 2 s  .c o  m
    MongoDatabase db = mongo.getDatabase();
    MongoIterable<String> collectionNames = db.listCollectionNames();

    for (String colName : collectionNames) {
        if (colName.equals("answerCollection"))
            collExist = true;
    }

    if (!collExist) {
        System.out.println("Creating collection: answerCollection");
        db.createCollection("answerCollection");
    }
    System.out.println("Answer migration : stop");
}

From source file:org.piotr.apollo.mongo.migration.FinishedMigration.java

public FinishedMigration() {

    System.out.println("Finished migration : start");

    Boolean collExist = false;/*from   ww  w  . j av a  2 s.  co m*/
    MongoDatabase db = mongo.getDatabase();
    MongoIterable<String> collectionNames = db.listCollectionNames();

    for (String colName : collectionNames) {
        if (colName.equals("finishedCollection"))
            collExist = true;
    }

    if (!collExist) {
        System.out.println("Creating collection: finishedCollection");
        db.createCollection("finishedCollection");
    }
    System.out.println("Finished migration : stop");
}

From source file:org.piotr.apollo.mongo.migration.LessonMigration.java

public LessonMigration() {

    System.out.println("Lesson migration : start");

    Boolean collExist = false;/*from ww w .  ja va2 s.  c o m*/
    MongoDatabase db = mongo.getDatabase();
    MongoIterable<String> collectionNames = db.listCollectionNames();

    for (String colName : collectionNames) {
        if (colName.equals("lessonCollection"))
            collExist = true;
    }

    if (!collExist) {
        System.out.println("Creating collection: lessonCollection");
        db.createCollection("lessonCollection");
    }
    System.out.println("Lesson migration : stop");
}

From source file:org.piotr.apollo.mongo.migration.QuestionMigration.java

public QuestionMigration() {

    System.out.println("Question migration : start");

    Boolean collExist = false;/*  w w w  . j a v  a2 s  .  c o m*/
    MongoDatabase db = mongo.getDatabase();
    MongoIterable<String> collectionNames = db.listCollectionNames();

    for (String colName : collectionNames) {
        if (colName.equals("questionCollection"))
            collExist = true;
    }

    if (!collExist) {
        System.out.println("Creating collection: questionCollection");
        db.createCollection("questionCollection");
    }
    System.out.println("Question migration : stop");
}

From source file:org.piotr.apollo.mongo.migration.SectionMigration.java

public SectionMigration() {

    System.out.println("Section migration : start");

    Boolean collExist = false;//from  w  w w.j a  v  a 2 s .  co  m
    MongoDatabase db = mongo.getDatabase();
    MongoIterable<String> collectionNames = db.listCollectionNames();

    for (String colName : collectionNames) {
        if (colName.equals("sectionCollection"))
            collExist = true;
    }

    if (!collExist) {
        System.out.println("Creating collection: sectionCollection");
        db.createCollection("sectionCollection");
    }
    System.out.println("Section migration : stop");
}