博客
关于我
一篇文章带你使用 Java 操作 MongoDB
阅读量:362 次
发布时间:2019-03-04

本文共 2819 字,大约阅读时间需要 9 分钟。

MongoDB Java 驱动操作指南

一、前期准备

首先需要下载并配置MongoDB的Java驱动。可以通过以下两种方式获取:

  • 通过Maven添加依赖

    在Maven项目中添加以下依赖项:

    org.mongodb
    mongodb-driver
    3.5.0

    如果手动下载,需要获取以下三个JAR包:

    • org.mongodb:bson:jar:3.5.0
    • org.mongodb:mongodb-driver-core:jar:3.5.0
    • org.mongodb:mongodb-driver:jar:3.5.0
  • 启动MongoDB

    在开始操作之前,请确保MongoDB服务已启动。

  • 二、获取集合

    完成前期准备后,使用MongoClient连接数据库并获取集合:

    MongoClient client = new MongoClient("192.168.248.136", 27017);MongoDatabase sang = client.getDatabase("sang");MongoCollection c = sang.getCollection("c1");

    三、CRUD 操作

    1. 增加操作

    插入单条数据:

    Document d1 = new Document();d1.append("name", "三国演义").append("author", "罗贯中");c.insertOne(d1);

    插入多条数据:

    List
    collections = new ArrayList<>();Document d1 = new Document();d1.append("name", "三国演义").append("author", "罗贯中");collections.add(d1);Document d2 = new Document();d2.append("name", "红楼梦").append("author", "曹雪芹");collections.add(d2);c.insertMany(collections);
    2. 修改操作

    修改单条数据:

    c.updateOne(Filters.eq("author", "罗贯中"), new Document("$set", new Document("name", "三国演义123")));

    修改多条数据:

    c.updateMany(Filters.eq("author", "罗贯中"), new Document("$set", new Document("name", "三国演义456")));
    3. 删除操作

    删除单条数据:

    c.deleteOne(Filters.eq("author", "罗贯中"));

    删除多条数据:

    c.deleteMany(Filters.eq("author", "罗贯中"));

    四、查询操作

    1. 查询所有文档
    FindIterable
    documents = c.find();MongoCursor
    iterator = documents.iterator();while (iterator.hasNext()) { System.out.println(iterator.next());}
    2. 按条件查询
    FindIterable
    documents = c.find(Filters.eq("author", "罗贯中"));MongoCursor
    iterator = documents.iterator();while (iterator.hasNext()) { System.out.println(iterator.next());}

    五、验证与配置

    1. 数据库验证

    如果需要验证数据库权限,使用以下方式:

    ServerAddress serverAddress = new ServerAddress("192.168.248.128", 27017);List
    credentialsList = new ArrayList<>();MongoCredential mc = MongoCredential.createScramSha1Credential("readuser", "sang", "123".toCharArray());credentialsList.add(mc);MongoClient client = new MongoClient(serverAddress, credentialsList);MongoDatabase sang = client.getDatabase("sang");MongoCollection c = sang.getCollection("c1");
    2. 连接配置

    可通过MongoClientOptions设置连接超时等参数:

    ServerAddress serverAddress = new ServerAddress("192.168.248.128", 27017);List
    credentialsList = new ArrayList<>();MongoCredential mc = MongoCredential.createScramSha1Credential("rwuser", "sang", "123".toCharArray());credentialsList.add(mc);MongoClientOptions options = MongoClientOptions.builder() .connectTimeout(1000 * 10) // 设置连接超时为10秒 .maxWaitTime(1000 * 10) // 设置最大等待时间为10秒 .build();MongoClient client = new MongoClient(serverAddress, credentialsList, options);MongoDatabase sang = client.getDatabase("sang");MongoCollection c = sang.getCollection("c1");

    通过以上步骤,您可以高效地进行MongoDB数据的增删改查操作,并通过合理配置实现对数据库的安全访问和连接管理。

    转载地址:http://odar.baihongyu.com/

    你可能感兴趣的文章
    Python-通过函数传递变量
    查看>>
    Python内置函数详解——总结篇
    查看>>
    Python.对象集合中的标识.和散列
    查看>>
    Python.硒.拖放错误“AttributeError:move_to 需要 WebElement“
    查看>>
    Python内置函数详解
    查看>>
    Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件
    查看>>
    Python/Kivy:我可以在双击标签时调用函数吗
    查看>>
    python内置函数5-frozenset()
    查看>>
    Python/Plotly:如何使用要显示的信息自定义悬停模板?
    查看>>
    Python/Selenium - 无法单击 www.instagram.com 上的“接受 cookie“按钮
    查看>>
    Python内置函数(35)——next
    查看>>
    python写入mysql
    查看>>
    python写入csv老是乱码或者各种不好
    查看>>
    Python内置函数
    查看>>
    Python2 安装MySQLdb库
    查看>>
    Python2 已终结,入手Python 3,你需要这30个技巧
    查看>>
    python写下拉列表在excel,无法使用python获取Excel下拉列表(组合框)值
    查看>>
    python2.7安装
    查看>>
    Python2.7安装ncmbot时提示:Microsoft Visual C++9.0 is required
    查看>>
    PYTHON2.Git.1
    查看>>