MongoDB java driver を 2系から、3系にupgrade したところ、
index 作成で以下のエラーが発生しました。
com.mongodb.MongoCommandException: Command failed with error 67: 'Values in v:2 index key pattern cannot be of type bool. Only numbers > 0, numbers < 0, and strings are allowed.' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "Values in v:2 index key pattern cannot be of type bool. Only numbers > 0, numbers < 0, and strings are allowed.", "code" : 67, "codeName" : "CannotCreateIndex" }
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:198)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:170)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:116)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:111)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
Values in v:2 index key pattern cannot be of type bool.
なので、
boolean 値は設定できないといっています。
実装上以下のように記載していました。
collection.createIndex(new BasicDBObject().append("mbid", 1).append("unique", true));
mongo-java-driver/DBCollection.java at 2.x · mongodb/mongo-java-driver
のcreateIndexの実装を見て見ると、
/**
* Forces creation of an index on a set of fields with the default options, if one does not already exist.
*
* @param keys a document that contains pairs with the name of the field or fields to index and order of the index
* @throws MongoException
* @mongodb.driver.manual /administration/indexes-creation/ Index Creation Tutorials
*/
public void createIndex( final DBObject keys ){
createIndex( keys , defaultOptions( keys ) );
}
そもそも、indexOption は、 指定できなかったらしく、
以前のversion ではエラーにならなかっただけだったことがわかりました。
version 3 からはIndexOptions が指定できるので、
以下の実装に書き換えました。
collection.createIndex(new BasicDBObject().append("mbid", 1), new IndexOptions().unique(true));
サーバー再起動して、変更を反映したところ正常動作するようになりました。
結構長いこと間違ったまま動いてたということになります。
以上です。
コメント