Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;

I want to run the following command to create a user with MongoDB Java Driver,

    client = new MongoClient(mongoClientURI);
    MongoDatabase database = client.getDatabase("db_1");

    Document createUserCommand = new Document();
    createUserCommand.put("createUser", "abc");
    createUserCommand.put("pwd", "abc");
    createUserCommand.put("roles", new String[]{"userAdmin", "read", "readWrite", "dbAdmin", "dbOwner"});         database.runCommand(createUserCommand);

But following exception occurred:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)

Looks the roles field which is an array leads to this problem, can some take a look at this problem? Thanks

like image 882
Tom Avatar asked Nov 23 '16 07:11

Tom


1 Answers

createUserCommand.put("roles", new String[]{"userAdmin", "read", "readWrite", "dbAdmin", "dbOwner"});
 database.runCommand(createUserCommand); 

should be

List<String> roles = new ArrayList<String>(); 
roles.add("userAdmin); ... createUserCommand.put("roles", roles); database.runCommand(createUserCommand);

Looks like it support List, not Array, which is really ugl

like image 70
Tom Avatar answered Nov 15 '22 08:11

Tom