Example usage for com.mongodb.async.client MongoDatabase createCollection

List of usage examples for com.mongodb.async.client MongoDatabase createCollection

Introduction

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

Prototype

void createCollection(String collectionName, SingleResultCallback<Void> callback);

Source Link

Document

Create a new collection with the given name.

Usage

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoCollection")
public MongoCollection mongoCollection() {
    System.out.println("*******************" + mycoll);

    final MongoDatabase mydb = mongoDatabase();

    /**/*www .java2s. co  m*/
     * ??,?;?;
     * 1???,?,????;
     */
    if (!collectionExists(mycoll)) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);//??

        mydb.createCollection(mycoll, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                // logger.debug("?");

                MongoCollection coll = mydb.getCollection(mycoll);

                coll.createIndex(new Document("position", "2d"), new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                //?
                Document expire = Document.parse("{time:1},{expireAfterSeconds:10*60}");
                coll.createIndex(expire, new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                countDownLatch.countDown();
            }
        });
        try {
            countDownLatch.await(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    MongoCollection coll = mydb.getCollection(mycoll);
    // TODO: 16/5/10  ;?
    //> db.location.ensureIndex( {position: "2d"} )

    return coll;
}