【教程】Java 集成Mongodb

【教程】Java 集成Mongodb

依赖

复制代码
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>3.12.14</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.20</version>
        </dependency>

MongoDB客户端封装-MongoDS | Hutool

配置文件 带认证 账号密码的

config/mongo.setting

复制代码
#每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住 ,默以为10 --int
connectionsPerHost=100
#线程队列数,它以connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出"Out of semaphores to get db"错误 --int
threadsAllowedToBlockForConnectionMultiplier=10
#被阻塞线程从连接池获取连接的最长等待时间(ms) --int
maxWaitTime = 120000
#在建立(打开)套接字连接时的超时时间(ms),默以为0(无穷) --int
connectTimeout=0
#套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
socketTimeout=0
#是否打开长连接. defaults to false --boolean
socketKeepAlive=false

#---------------------------------- MongoDB实例连接
[master]
host = 127.0.0.1:27017
user = root
pass = 123456
database = admin



# [slave]
# host = 127.0.0.1:27018
#-----------------------------------------------------

使用方法

复制代码
MongoDatabase db = MongoFactory.getDS("master").getDb("test");

修改

复制代码
MongoDatabase db = WTool.mongoGetDefDb();
Bson filter = Filters.in("_id", ids);
Bson update = Updates.set("status", -2);
//        UpdateOptions options = new UpdateOptions().arrayFilters(Arrays.asList(Filters.eq("array_filter_name.field_name", "field_value")));

db.getCollection("res_phone").updateMany(filter, update);
相关推荐
midsummer_woo6 分钟前
基于spring boot的纺织品企业财务管理系统(源码+论文)
java·spring boot·后端
zc-code22 分钟前
Spring Boot + @RefreshScope:动态刷新配置的终极指南
java·spring boot·后端
何中应26 分钟前
EasyExcel使用(二:写出)
java·后端·maven·excel
awonw35 分钟前
[python][基础]Flask 技术栈
开发语言·python·flask
木宇(记得热爱生活)1 小时前
Qt GUI缓存实现
开发语言·qt·缓存
lly2024061 小时前
C# 正则表达式
开发语言
Chef_Chen1 小时前
从0开始学习R语言--Day58--竞争风险模型
android·开发语言·kotlin
minji...1 小时前
数据结构 堆(4)---TOP-K问题
java·数据结构·算法
咖啡の猫1 小时前
bash的特性-常见的快捷键
开发语言·chrome·bash
命苦的孩子1 小时前
Java 中的排序算法详解
java·开发语言·排序算法