org.bson.codecs.configuration.CodecConfigurationException:找不到 class [Ljava.lang.String 的编解码器;
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.String;
我想运行下面的命令用MongoDBJava驱动创建一个用户,
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);
但出现以下异常:
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)
看是roles字段是数组导致的这个问题,有大佬看看这个问题吗?谢谢
createUserCommand.put("roles", new String[]{"userAdmin", "read", "readWrite", "dbAdmin", "dbOwner"});
database.runCommand(createUserCommand);
应该是
List<String> roles = new ArrayList<String>();
roles.add("userAdmin); ... createUserCommand.put("roles", roles); database.runCommand(createUserCommand);
貌似支持List,不支持Array,真丑
我想运行下面的命令用MongoDBJava驱动创建一个用户,
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);
但出现以下异常:
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)
看是roles字段是数组导致的这个问题,有大佬看看这个问题吗?谢谢
createUserCommand.put("roles", new String[]{"userAdmin", "read", "readWrite", "dbAdmin", "dbOwner"});
database.runCommand(createUserCommand);
应该是
List<String> roles = new ArrayList<String>();
roles.add("userAdmin); ... createUserCommand.put("roles", roles); database.runCommand(createUserCommand);
貌似支持List,不支持Array,真丑