I have a document that has a nested document set as the "name" variable:
Document{{name=Document{{he=דמטריוס הכרונוגראף, en=Demetrius the Chronographer}}}}
It is contained in a mock database that I am meant to implement search functionality into. I would like to Index the "name" field
of the documents in my collection to quicken the time complexity of a standard collection.find()
function, but my name field is set as a Document in which the multiple names are contained in an array.
I would like to be able to index the "he" variant, and "en" variant separately so I can search both fields, but am not sure how to do that via the Java driver. Currently, I am trying to use:
db.getCollection(collectioName).createIndex(text(name.en));
However, I get an error message that reads:
Exception in thread "main" com.mongodb.MongoCommandException: Command failed with error 67 (CannotCreateIndex): 'Error in specification { key: { $text: { $search: "name.en" } }, name: "$text_", ns: "60d4a208049d3506e1c36083_alhatorah.people" } :: caused by :: Values in v:2 index key pattern cannot be of type object. Only numbers > 0, numbers < 0, and strings are allowed.' on server cluster0-shard-00-02.l1p7n.mongodb.net:27017. The full response is { "operationTime" : { "$timestamp" : { "t" : 1625086457, "i" : 22 } }, "ok" : 0.0, "errmsg" : "Error in specification { key: { $text: { $search: \"name.en\" } }, name: \"$text_\", ns: \"60d4a208049d3506e1c36083_alhatorah.people\" } :: caused by :: Values in v:2 index key pattern cannot be of type object. Only numbers > 0, numbers < 0, and strings are allowed.", "code" : 67, "codeName" : "CannotCreateIndex", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1625086457, "i" : 22 } }, "signature" : { "hash" : { "$binary" : "BCifcktqC8uoPcR7ncgMmas8qY8=", "$type" : "00" }, "keyId" : { "$numberLong" : "6930011667187105794" } } } }
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.sendAndReceive(UsageTrackingInternalConnection.java:99)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.sendAndReceive(DefaultConnectionPool.java:444)
at com.mongodb.internal.connection.CommandProtocolImpl.execute(CommandProtocolImpl.java:72)
at com.mongodb.internal.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:200)
at com.mongodb.internal.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:269)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:131)
at com.mongodb.internal.connection.DefaultServerConnection.command(DefaultServerConnection.java:123)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:242)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:233)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:170)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:163)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:175)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:170)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:453)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:415)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:170)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:70)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:193)
at com.mongodb.client.internal.MongoCollectionImpl.executeCreateIndexes(MongoCollectionImpl.java:805)
at com.mongodb.client.internal.MongoCollectionImpl.createIndexes(MongoCollectionImpl.java:788)
at com.mongodb.client.internal.MongoCollectionImpl.createIndexes(MongoCollectionImpl.java:783)
at com.mongodb.client.internal.MongoCollectionImpl.createIndex(MongoCollectionImpl.java:768)
at com.mongodb.client.internal.MongoCollectionImpl.createIndex(MongoCollectionImpl.java:763)
at DatabaseManager.createIndex(DatabaseManager.java:57)
at Main.main(Main.java:23)
Process finished with exit code 1
What can I do to Index the name field here?