嗨,我在 mongodb 上尝试了一个连接到 mongohq 的程序,但出现以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1958)
at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:186)
at com.mongodb.MongoClientURI.<init>(MongoClientURI.java:138)
at org.Mongoadded.main(Mongoadded.java:15)
我返回的代码是:
package org;
import java.net.UnknownHostException;
import java.util.Set;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
public class Mongoadded {
public static void main(String args[]) throws UnknownHostException{
String texturi="mongodb://mymail@gmail.com:password@emma.mongohq.com:10045/mongo";
MongoClientURI mongoURI=new MongoClientURI(texturi);
MongoClient mongoclient=new MongoClient(mongoURI);
DB db=mongoclient.getDB("bhv");
db.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
Set<String> colls=db.getCollectionNames();
for(String c:colls){
System.out.println(c);
}
}
我使用的 mongo-java-driver 是 2.10.1 。先感谢您