public static void main(final String[] args) throws InterruptedException {
        MongoClient mongoClient;

        if (args.length == 0) {
            // connect to the local database server
            mongoClient = MongoClients.create();
        } else {
            mongoClient = MongoClients.create(args[0]);
        }

        // get handle to "mydb" database
        MongoDatabase database = mongoClient.getDatabase("mydb");


        // get a handle to the "test" collection
        MongoCollection<Document> collection = database.getCollection("test");
        final CountDownLatch dropLatch = new CountDownLatch(1);
        collection.drop(new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                dropLatch.countDown();
            }
        });
        dropLatch.await();

        // getting a list of databases
        SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                System.out.println("Operation Finished!");
            }
        };

        mongoClient.listDatabaseNames().forEach(new Block<String>() {
            @Override
            public void apply(final String s) {
                System.out.println(s);
            }
        }, callbackWhenFinished);

        // drop a database
        mongoClient.getDatabase("databaseToBeDropped").drop(callbackWhenFinished);

        // create a collection
        database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000),
                callbackWhenFinished);


        database.listCollectionNames().forEach(new Block<String>() {
            @Override
            public void apply(final String databaseName) {
                System.out.println(databaseName);
            }
        }, callbackWhenFinished);

        // drop a collection:
        collection.drop(callbackWhenFinished);

        // create an ascending index on the "i" field
        collection.createIndex(Indexes.ascending("i"), new SingleResultCallback<String>() {
            @Override
            public void onResult(final String result, final Throwable t) {
                System.out.println("Operation finished");
            }
        });

        // list the indexes on the collection
        Block<Document> printDocumentBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
        collection.listIndexes().forEach(printDocumentBlock, callbackWhenFinished);


        // create a text index on the "content" field
        collection.createIndex(Indexes.text("content"), new SingleResultCallback<String>() {
            @Override
            public void onResult(final String result, final Throwable t) {
                System.out.println("Operation finished");
            }
        });

        collection.insertOne(new Document("_id", 0).append("content", "textual content"), callbackWhenFinished);
        collection.insertOne(new Document("_id", 1).append("content", "additional content"), callbackWhenFinished);
        collection.insertOne(new Document("_id", 2).append("content", "irrelevant content"), callbackWhenFinished);

        // Find using the text index
        collection.countDocuments(text("textual content -irrelevant"), new SingleResultCallback<Long>() {
            @Override
            public void onResult(final Long matchCount, final Throwable t) {
                System.out.println("Text search matches: " + matchCount);
            }
        });


        // Find using the $language operator
        Bson textSearch = text("textual content -irrelevant", new TextSearchOptions().language("english"));
        collection.countDocuments(textSearch, new SingleResultCallback<Long>() {
            @Override
            public void onResult(final Long matchCount, final Throwable t) {
                System.out.println("Text search matches (english): " + matchCount);
            }
        });

        // Find the highest scoring match
        Document projection = new Document("score", new Document("$meta", "textScore"));
        collection.find(textSearch).projection(projection).first(new SingleResultCallback<Document>() {
            @Override
            public void onResult(final Document highest, final Throwable t) {
                System.out.println("Highest scoring document: " + highest.toJson());

            }
        });

        // Run a command
        database.runCommand(new Document("buildInfo", 1), new SingleResultCallback<Document>() {
            @Override
            public void onResult(final Document buildInfo, final Throwable t) {
                System.out.println(buildInfo);
            }
        });

        // release resources
        database.drop(callbackWhenFinished);
        mongoClient.close();
    }
public static void main(final String[] args) throws InterruptedException {
        MongoClient mongoClient;

        if (args.length == 0) {
            
            mongoClient = MongoClients.create();
        } else {
            mongoClient = MongoClients.create(args[0]);
        }

        
        MongoDatabase database = mongoClient.getDatabase("mydb");


        
        MongoCollection<Document> collection = database.getCollection("test");
        final CountDownLatch dropLatch = new CountDownLatch(1);
        collection.drop(new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                dropLatch.countDown();
            }
        });
        dropLatch.await();

        
        SingleResultCallback<Void> callbackWhenFinished = new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                System.out.println("Operation Finished!");
            }
        };

        mongoClient.listDatabaseNames().forEach(new Block<String>() {
            @Override
            public void apply(final String s) {
                System.out.println(s);
            }
        }, callbackWhenFinished);

        
        mongoClient.getDatabase("databaseToBeDropped").drop(callbackWhenFinished);

        
        database.createCollection("cappedCollection", new CreateCollectionOptions().capped(true).sizeInBytes(0x100000),
                callbackWhenFinished);


        database.listCollectionNames().forEach(new Block<String>() {
            @Override
            public void apply(final String databaseName) {
                System.out.println(databaseName);
            }
        }, callbackWhenFinished);

        
        collection.drop(callbackWhenFinished);

        
        collection.createIndex(Indexes.ascending("i"), new SingleResultCallback<String>() {
            @Override
            public void onResult(final String result, final Throwable t) {
                System.out.println("Operation finished");
            }
        });

        
        Block<Document> printDocumentBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
        collection.listIndexes().forEach(printDocumentBlock, callbackWhenFinished);


        
        collection.createIndex(Indexes.text("content"), new SingleResultCallback<String>() {
            @Override
            public void onResult(final String result, final Throwable t) {
                System.out.println("Operation finished");
            }
        });

        collection.insertOne(new Document("_id", 0).append("content", "textual content"), callbackWhenFinished);
        collection.insertOne(new Document("_id", 1).append("content", "additional content"), callbackWhenFinished);
        collection.insertOne(new Document("_id", 2).append("content", "irrelevant content"), callbackWhenFinished);

        
        collection.countDocuments(text("textual content -irrelevant"), new SingleResultCallback<Long>() {
            @Override
            public void onResult(final Long matchCount, final Throwable t) {
                System.out.println("Text search matches: " + matchCount);
            }
        });


        
        Bson textSearch = text("textual content -irrelevant", new TextSearchOptions().language("english"));
        collection.countDocuments(textSearch, new SingleResultCallback<Long>() {
            @Override
            public void onResult(final Long matchCount, final Throwable t) {
                System.out.println("Text search matches (english): " + matchCount);
            }
        });

        
        Document projection = new Document("score", new Document("$meta", "textScore"));
        collection.find(textSearch).projection(projection).first(new SingleResultCallback<Document>() {
            @Override
            public void onResult(final Document highest, final Throwable t) {
                System.out.println("Highest scoring document: " + highest.toJson());

            }
        });

        
        database.runCommand(new Document("buildInfo", 1), new SingleResultCallback<Document>() {
            @Override
            public void onResult(final Document buildInfo, final Throwable t) {
                System.out.println(buildInfo);
            }
        });

        
        database.drop(callbackWhenFinished);
        mongoClient.close();
    }
public static void main ( final String [ ] args ) throws InterruptedException { MongoClient mongoClient ; if ( args . length == 0 ) { // connect to the local database server mongoClient = MongoClients . create ( ) ; } else { mongoClient = MongoClients . create ( args [ 0 ] ) ; } // get handle to "mydb" database MongoDatabase database = mongoClient . getDatabase ( "mydb" ) ; // get a handle to the "test" collection MongoCollection < Document > collection = database . getCollection ( "test" ) ; final CountDownLatch dropLatch = new CountDownLatch ( 1 ) ; collection . drop ( new SingleResultCallback < Void > ( ) { @ Override public void onResult ( final Void result , final Throwable t ) { dropLatch . countDown ( ) ; } } ) ; dropLatch . await ( ) ; // getting a list of databases SingleResultCallback < Void > callbackWhenFinished = new SingleResultCallback < Void > ( ) { @ Override public void onResult ( final Void result , final Throwable t ) { System . out . println ( "Operation Finished!" ) ; } } ; mongoClient . listDatabaseNames ( ) . forEach ( new Block < String > ( ) { @ Override public void apply ( final String s ) { System . out . println ( s ) ; } } , callbackWhenFinished ) ; // drop a database mongoClient . getDatabase ( "databaseToBeDropped" ) . drop ( callbackWhenFinished ) ; // create a collection database . createCollection ( "cappedCollection" , new CreateCollectionOptions ( ) . capped ( true ) . sizeInBytes ( 0x100000 ) , callbackWhenFinished ) ; database . listCollectionNames ( ) . forEach ( new Block < String > ( ) { @ Override public void apply ( final String databaseName ) { System . out . println ( databaseName ) ; } } , callbackWhenFinished ) ; // drop a collection: collection . drop ( callbackWhenFinished ) ; // create an ascending index on the "i" field collection . createIndex ( Indexes . ascending ( "i" ) , new SingleResultCallback < String > ( ) { @ Override public void onResult ( final String result , final Throwable t ) { System . out . println ( "Operation finished" ) ; } } ) ; // list the indexes on the collection Block < Document > printDocumentBlock = new Block < Document > ( ) { @ Override public void apply ( final Document document ) { System . out . println ( document . toJson ( ) ) ; } } ; collection . listIndexes ( ) . forEach ( printDocumentBlock , callbackWhenFinished ) ; // create a text index on the "content" field collection . createIndex ( Indexes . text ( "content" ) , new SingleResultCallback < String > ( ) { @ Override public void onResult ( final String result , final Throwable t ) { System . out . println ( "Operation finished" ) ; } } ) ; collection . insertOne ( new Document ( "_id" , 0 ) . append ( "content" , "textual content" ) , callbackWhenFinished ) ; collection . insertOne ( new Document ( "_id" , 1 ) . append ( "content" , "additional content" ) , callbackWhenFinished ) ; collection . insertOne ( new Document ( "_id" , 2 ) . append ( "content" , "irrelevant content" ) , callbackWhenFinished ) ; // Find using the text index collection . countDocuments ( text ( "textual content -irrelevant" ) , new SingleResultCallback < Long > ( ) { @ Override public void onResult ( final Long matchCount , final Throwable t ) { System . out . println ( "Text search matches: " + matchCount ) ; } } ) ; // Find using the $language operator Bson textSearch = text ( "textual content -irrelevant" , new TextSearchOptions ( ) . language ( "english" ) ) ; collection . countDocuments ( textSearch , new SingleResultCallback < Long > ( ) { @ Override public void onResult ( final Long matchCount , final Throwable t ) { System . out . println ( "Text search matches (english): " + matchCount ) ; } } ) ; // Find the highest scoring match Document projection = new Document ( "score" , new Document ( "$meta" , "textScore" ) ) ; collection . find ( textSearch ) . projection ( projection ) . first ( new SingleResultCallback < Document > ( ) { @ Override public void onResult ( final Document highest , final Throwable t ) { System . out . println ( "Highest scoring document: " + highest . toJson ( ) ) ; } } ) ; // Run a command database . runCommand ( new Document ( "buildInfo" , 1 ) , new SingleResultCallback < Document > ( ) { @ Override public void onResult ( final Document buildInfo , final Throwable t ) { System . out . println ( buildInfo ) ; } } ) ; // release resources database . drop ( callbackWhenFinished ) ; mongoClient . close ( ) ; }
Run this main method to see the output of this quick example.

@param args takes an optional single argument for the connection string
@throws InterruptedException if a latch is interrupted
Run this main method to see the output of this quick example .