2014-12-04 88 views

回答

1

看看Java Mongo Driver(http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started-with-java-driver)。具體來說,對於你的情況,你需要創建一個mongoClient接種你的mongos的主機和端口(或者mongos-es?在這裏閱讀更多http://api.mongodb.org/java/current/com/mongodb/MongoClient.html)。

// for a sharded cluster setup with two mongos running on 27017 and 27018 
MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017), 
             new ServerAddress("localhost", 27018)); 

DB db = mongoClient.getDB("mydb"); 

如果你正在建設有關MongoDB之上的更復雜的應用規劃,我強烈建議看嗎啡(https://github.com/mongodb/morphia)爲好。