使用Java操作MongoDB

  1. 首先,下载mongoDB对Java支持的驱动包 
        驱动包下载

  2. 下面建立一个JavaProject工程,导入下载下来的驱动包。即可在Java中使用mongoDB.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class MongoDb {
    public static void main(String[] args) throws UnknownHostException {
    MongoClient mg = new MongoClient();
    DB db = mg.getDB("CRUD");

    DBCollection users = db.getCollection("test");
    DBCursor cur = users.find();

    while (cur.hasNext()) {
    System.out.println(cur.next());
    }

    System.out.println(cur.count());
    System.out.println(cur.getCursorId());
    System.out.println(JSON.serialize(cur));
    }
    }
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!