Example usage for com.mongodb.async.client MongoCollection createIndex

List of usage examples for com.mongodb.async.client MongoCollection createIndex

Introduction

In this page you can find the example usage for com.mongodb.async.client MongoCollection createIndex.

Prototype

void createIndex(Bson key, SingleResultCallback<String> callback);

Source Link

Document

Creates an index.

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

    /**//from ww w  . j a  v a  2s . c om
     * ??,?;?;
     * 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;
}