mongo操作源码
package mongo; import java.util.ArrayList; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoDatabase; public class chuangjianjihe { public static void main(String[] args) { try { ServerAddress serverAddress =new ServerAddress("localhost",27017); ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>(); addrs.add(serverAddress); MongoCredential credential = MongoCredential.createScramSha1Credential("root","admin"," ".toCharArray()); ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>(); credentials.add(credential); MongoClient mongoClient = new MongoClient(addrs,credentials); MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); System.out.println("Connect to database successfully"); mongoDatabase.createCollection("mycol"); System.out.println("Collection mycol was created successfully"); }catch (Exception e){ System.err.println(e.getClass().getName()+":"+e.getMessage()); } } }