MongoDB の java driver 経由で Index の 作成、削除 する処理を実装していて、 興味深い動作をしていたので、記載します。


Mongo Driver の Version

3.2.2 です。

    <!-- MongoDB -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.2.2</version>
    </dependency>


Index の作成

    // createdAt_ttl_index
    IndexOptions createdAtIndexOptions =
            new IndexOptions()
                    .expireAfter(settings.getExpirationTime(), TimeUnit.MINUTES)
                    .background(true)
                    .name("createdAt_ttl_index");
    Bson createdAt = Indexes.ascending("createdAt");
    collection.createIndex(createdAt, createdAtIndexOptions);

上記で、既に作成済の場合は、特にExceptionが出力されるわけでもなく、
再作成はされずに、正常終了しました。


Index の削除

    collection.dropIndex("aaaaaa");
Index 名で削除する場合は、Indexが存在しない場合、下記エラーになりました。
! com.mongodb.MongoCommandException: Command failed with error 27: 'index not found with name [aaaaaa]' on server localhost:27017. The full response is { "nIndexesWas" : 3, "ok" : 0.0, "errmsg" : "index not found with name [aaaaaa]", "code" : 27, "codeName" : "IndexNotFound" }
! at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:115)
! at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
! at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
! at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
! at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:186)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:160)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:133)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:128)
! at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:118)
! at com.mongodb.operation.DropIndexOperation.execute(DropIndexOperation.java:69)
! at com.mongodb.operation.DropIndexOperation.execute(DropIndexOperation.java:40)


Indexが存在しない場合は、作成する

    if (!isExistsIndex("createdAt_ttl_index")) {
        // createdAt_ttl_index
        IndexOptions createdAtIndexOptions =
                new IndexOptions()
                        .expireAfter(settings.getExpirationTime(), TimeUnit.MINUTES)
                        .background(true)
                        .name("createdAt_ttl_index");
        Bson createdAt = Indexes.ascending("createdAt");
        collection.createIndex(createdAt, createdAtIndexOptions);
    }
    ......
    private boolean isExistsIndex(String indexName) {
        Args.notNull(indexName, "indexName");
        ListIndexesIterable<Document> indexes = collection.listIndexes();
        for (Document document : indexes) {
            String name = document.getString("name");
            if (indexName.equals(name)) {
                return true;
            }
        }
        return false;
    }

MongoCollection#listIndexes() メソッド で collection が保持するindex が取得できるので、
取得後、Loop で 名称の一致判定をして、存在確認ができます。
createIndex挙動からいって、使うことはないかもしれません。 1
1.実装が終わってから、createIndex挙動に気づきました。。

以上です。

コメント