MongDBjava连接简单实例

默认情况.MongoDB服务./mongod   开启服务是只能本机访问 ,需要开放所有ip或者指定连接的ip地址./bin/mongod -bind_ip_all

public static void main(String[] args) {
        try {
            MongoClient mongoClient = new MongoClient("192.168.25.128", 27017);
            MongoDatabase database = mongoClient.getDatabase("test");
            System.out.println("连接成功");
            MongoCollection<Document> ldw = database.getCollection("ldw");
            Document first = ldw.find().first();
            System.out.println(first.toJson());


        } catch (Exception e) {
            System.out.println(e.getClass().getName() + ":" + e.getMessage());
        }
    }

 

posted @ 2018-03-07 09:58  java王子  阅读(136)  评论(0编辑  收藏  举报