目录
基础
连接串
applicaiton.yml
spring:
data:
mongodb:
法1
# uri: mongodb://127.0.0.1:27017/bookmark
法2
# host: 127.0.0.1
# port: 27017
# database: bookmark
# username: admin
# password: ENC(btzFqbKRth0UfO2iuY+KgQ==)
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
# local
# url: jdbc:mysql://localhost:3306/bookmarksdb3?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
# username: root
## password: "123456"
# password: ENC(4DE4bFfmmsbqN/X12lrbwQ==)
# test
# url: jdbc:mysql://7.205.116.205:3306/book?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
# username: dbAdmin
# password: ENC(ato8fgnU10FXdw9JQyYbFEL0ZIK18TJF)
server:
port: 8094
jasypt:
encryptor:
password: sad124f1f1rf1fgt5
property:
prefix: ENC(
suffix: )
MongoDB 设置用户名密码和数据库连接
https://blog.csdn.net/qq_42583263/article/details/121403125
command create requires authentication(MongoDB 创建集合失败)
https://blog.csdn.net/cc0429xy/article/details/108877381
https://www.panyanbin.com/article/c602b9e2.html
use admin
db.createUser({
user: 'admin', // 用户名
pwd: '123456', // 密码
roles:[{
role: 'root', // 角色
db: 'admin' // 数据库
}]
})
db.createUser({
user: "testUser",
pwd: "Da123654789",
roles : [{
role: "readWrite",
db: "bookmark"
}]
});
清空表数据
db.集合名.drop();
查询数据量
db.集合名.count();
db.getCollection("集合名").find().count();
开启认证
https://segmentfault.com/a/1190000021616620
https://blog.csdn.net/yujia_666/article/details/115111942
语法
TRUNCATE(X,D)
小数x 截断为d位小数位数
SELECT TRUNCATE(123.4567, 3); # 123.456
SELECT TRUNCATE(123.4567, 2); # 123.45
SELECT TRUNCATE(123.4567, 1); # 123.4
SELECT TRUNCATE(123.4567, 0); # 123
SELECT TRUNCATE(123.4567, -1); # 120
SELECT TRUNCATE(123.4567, -2); # 100
SELECT TRUNCATE(123.4567, -3); # 0
SELECT TRUNCATE(-123.4567, 3); # -123.456
SELECT TRUNCATE(-123.4567, 2); # -123.45
SELECT TRUNCATE(-123.4567, 1); # -123.4
SELECT TRUNCATE(-123.4567, 0); # -123
SELECT TRUNCATE(-123.4567, -1); # -120
SELECT TRUNCATE(-123.4567, -2); # -100
SELECT TRUNCATE(-123.4567, -3); # 0
mysql 查询
https://blog.csdn.net/zuihongyan518/article/details/80936604
1
https://blog.csdn.net/qq_37870582/article/details/107024671
![](https://i-blog.csdnimg.cn/blog_migrate/398eb9891665983e70ddd51cfe7da7e7.png)
简单例子1
https://www.digitalocean.com/community/tutorials/mongodb-java-crud-example-tutorial
简单例子2
普通查询
https://www.cnblogs.com/lyc94620/p/12148882.html
https://developer.aliyun.com/article/443190
语法
https://www.mongodb.com/docs/manual/reference/sql-aggregation-comparison/
连表查询
相查询 在表a中有,表b没有的记录
![](https://i-blog.csdnimg.cn/blog_migrate/31d730f74f8bc227923cfe27a3212809.webp?x-image-process=image/format,png)
DataGrip的转换功能
https://blog.csdn.net/qq_39935047/article/details/107450151
$ne = not equals 不等于
$eq = equals 等于
# 查询某字段不为空的数据
db.hfijf.find({fieldName: {$ne:null}})
# 查询字段等于空的数据
db.hfijf.find({fieldName: {$eq:null}})
$exists:表示是否存在。值为false表示不存在,值为true表示存在
# 查询某字段不为空的数据
db.fdafdsa.find({fieldName:{$exists:true}})
# /查询某字段不存在的数据
db.fdafdsa.find({fieldName:{$exists:false}})
SELECT * FROM `bookmarks` LEFT JOIN `transfer`
on bookmarks.bookmarkId = transfer.id where transfer.flag is null limit 4;
db.bookmarks.aggregate( [
{
$lookup:{
from: "transfer",
localField: "bookmark_id",
foreignField:"_id",
as:"newTransfer"
}
},
{
$match: {
newTransfer:{$ne:null}
}
},
{
$skip:1900
},
{
$limit:500
}
] )
aggregation = Aggregation.newAggregation(
// 关联member表
Aggregation.lookup(
"transfer", // 从表 表名
"bookmark_id", // 如bookmarks被查询主表的bookmark_id
"_id", // 如transfer从表的主键_id
"flag" // 联合查询出的别名,用于多条件查询表明前缀,相当于SQL中的临时表名
),
Aggregation.unwind("transfer",true),
// 查询条件
Aggregation.match(
Criteria.where("flag.id").ne(null) //若没有这个条件,那后面就要跳转
),
// 分页:页码, 没有上一行条件,这里就要跳转前面不合规的数据
// Aggregation.skip((i * PAGE_LIMIT)),
// 分页:每次只查500条数据
Aggregation.limit(PAGE_LIMIT)
);
List<BookmarkEntity> mongoList1 =
mongoTemplate.aggregate(aggregation, "bookmarks", BookmarkEntity.class).getMappedResults();
上面代码 flag = 联合查询后 联合的临时表 的名字
BookmarkEntity 需要包含 transfer对应的list<flag>
类
统计数量
https://blog.csdn.net/interestANd/article/details/119903007
https://www.jianshu.com/p/6317d145fde4
https://life.caiguoyu.cn/articles/114
https://www.runoob.com/mongodb/mongodb-aggregate.html
SELECT COUNT(*) AS count
FROM orders
db.orders.aggregate( [
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
] )
Aggregation aggregation = Aggregation.newAggregation(
// 关联member表
Aggregation.lookup(
"transfer", // 从表 表名
"bookmark_id", // 如bookmarks被查询主表的bookmark_id
"_id", // 如transfer从表的主键_id
"flag" // 联合查询出的别名,用于多条件查询表明前缀,相当于SQL中的临时表名
),
Aggregation.group("_id").count().as("sum")
);
AggregationResults<Map> t = mongoTemplate.aggregate(aggregation,"bookmarks",Map.class);
对于百万级别的数据,连表查询:
// 查询数量
Aggregation aggregation =
Aggregation.newAggregation(
// 关联member表
Aggregation.lookup(
"transfer", // 从表 表名
"bookmark_id", // 如bookmarks被查询主表的bookmark_id
"_id", // 如transfer从表的主键_id
"flag" // 联合查询出的别名,用于多条件查询表明前缀,相当于SQL中的临时表名
),
Aggregation.unwind("transfer", true),
// 查询条件
Aggregation.match(Criteria.where("flag.id").ne(null)),
Aggregation.group("_id").count().as("sum"));
AggregationResults<Map> amountTemp = mongoTemplate.aggregate(aggregation, "bookmarks", Map.class);
报错:
Command failed with error 292 (QueryExceededMemoryLimitNoDiskUseAllowed): ‘PlanExecutor error during aggregation :: caused by :: Exceeded memory limit for $group, but didn’t allow external sort. Pass allowDiskUse:true to opt in.’ on server 127.0.0.1:27017. The full response is {“ok”: 0.0, “errmsg”: “PlanExecutor error during aggregation :: caused by :: Exceeded memory limit for $group, but didn’t allow external sort. Pass allowDiskUse:true to opt in.”, “code”: 292, “codeName”: “QueryExceededMemoryLimitNoDiskUseAllowed”}
超过$group的内存限制,但不允许外部排序
在服务器127.0.0.1:27017上传递allowDiskUse:true以选择加入
https://blog.csdn.net/qq_42543063/article/details/111380373
AggregationOptions aggregationOptions = AggregationOptions.builder().allowDiskUse(true).build();
// 查询数量
Aggregation aggregation =
Aggregation.newAggregation(
// 关联member表
Aggregation.lookup(
"transfer", // 从表 表名
"bookmark_id", // 如bookmarks被查询主表的bookmark_id
"_id", // 如transfer从表的主键_id
"flag" // 联合查询出的别名,用于多条件查询表明前缀,相当于SQL中的临时表名
),
Aggregation.unwind("transfer", true),
// 查询条件
Aggregation.match(Criteria.where("flag.id").ne(null)),
Aggregation.group("_id").count().as("sum")
).withOptions(aggregationOptions);
java mongo Criteria
https://www.baeldung.com/queries-in-spring-data-mongodb
java mongo 连表查询
https://blog.csdn.net/seesun2012/article/details/105165495
java mongo 聚合Aggregation
https://www.cnblogs.com/wangzhebin/p/16494929.html
简单查询 更新
db.getCollection("bookmarks").find({"w3Account":"c002787255test"})
https://www.runoob.com/mongodb/mongodb-update.html
全部更新
首选:
db.getCollection("bookmarks").updateMany(
{"w3Account":"c002787255test"},
{
$set :
{
"w3Account":"c002787255"
}
}
)
次选:
db.getCollection("bookmarks").update(
{"w3Account":"c002787255test"},
{
$set :
{
"w3Account":"c002787255"
}
},
false,true
)
spring:
data:
mongodb:
uri: mongodb://127.0.0.1:27017/Mongo
Mongo是数据库名 见下图
Query query = new Query(Criteria.where("name").is("asd1"));
Update update = new Update().set("name","asd1wer");
mongoTemplate.updateMulti(query, update, data.class,"testdata");
![](https://i-blog.csdnimg.cn/blog_migrate/eb0cea4d249c2da966c5a9c2bc328570.png)
mongo 优化
https://blog.csdn.net/qq_33589510/article/details/104924211
mongodb
查询
https://blog.csdn.net/weixin_43113679/article/details/91875707
(大于,小于) 模糊查询
查询 transfer表中 _id最大的一项
db.getCollection("transfer").find().sort({ id: -1}).limit(1);
查询 _id>65585edc3b4d2b3fbfe03a41 的数量
db.getCollection("bookmarks").find({_id:{$gt:ObjectId("65585edc3b4d2b3fbfe03a41")}}).count();
mongo 手动插入的数据我们无法手动修改其_id
行不通的:
http://blog.itpub.net/1727/viewspace-2833249/
var o = db.class.findOne( { "_id": ObjectId("5405a3b0acff6a440a000000")});
o._id = ObjectId("53fc5705acff6a7a6b000000");
db.class.save(o);
https://blog.51cto.com/u_16099226/6408994
查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.userInfo.find({name: /mongo/});//相当于%%
select * from userInfo where name like ‘%mongo%’;
查询 name 中以 mongo 开头的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%’;
查询指定列 name、 age 数据
db.userInfo.find({}, {name: 1, age: 1});
前一个大括号写查询条件,后一个大括号用于指定某些列进行显示
相当于: select name, age from userInfo;
name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,
如果用 false 就是排除 name,显示 name 以外的列信息
按照年龄排序 1 升序 -1 降序
升序: db.userInfo.find().sort({age: 1});
降序: db.userInfo.find().sort({age: -1});