创建集合
新建集合
![0](http://dev-img.mos.moduyun.com/20231215/f8b29d97-3e00-489c-af30-676c11d260e4.png)
点击保存
![0](http://dev-img.mos.moduyun.com/20231215/9acf9040-8bfd-47b9-b79c-1646bbb04c6b.png)
给集合起名字
![0](http://dev-img.mos.moduyun.com/20231215/e5f5566e-cf12-4ff3-8713-b6fedcc9ce6d.png)
创建一个查询
新增
-- 新增数据2种方式
![0](http://dev-img.mos.moduyun.com/20231215/89a0fd3f-f6bb-40bd-b18e-ca2a5e20cb93.png)
db.mongodb_test.save({"name":"stringBoot"})
db.mongodb_test.insert({"name":"mango good"});
db.mongodb_test.save({"name":"mango good",type:"工具书"});
![0](http://dev-img.mos.moduyun.com/20231215/09e00845-cfac-4a72-956f-9d181085019c.png)
-- 新增多条数据
db.mongodb_test.insert([
{name:"stringboot",age:17,sex:"男"},
{name:"srting",age:18,sex:"女"},
{name:"stringmvc",age:17,sex:"男"}
])
查询
-- 查询所有数据2种写法
db.mongodb_test.find() //查询全部 相当于:select* from user;
![0](http://dev-img.mos.moduyun.com/20231215/4e1ddb22-66d3-4b61-abd0-daef960f3159.png)
![0](http://dev-img.mos.moduyun.com/20231215/9148e9df-3db1-4913-9f7c-02b3ff20b323.png)
-- 查询去掉后的当前聚集集合中的某列的重复数据
db.mongodb_test.distinct("name") //会过滤掉 name 中的相同数据 相当于:select distict name from user;
![0](http://dev-img.mos.moduyun.com/20231215/ff2ca4b8-9edb-4769-b31a-7761ff024f8d.png)
![0](http://dev-img.mos.moduyun.com/20231215/d98f96c1-0375-441b-8537-eb5dda3ed8c5.png)
-- 查询 age = 17 的记录
db.mongodb_test.find({"age":17}) //相当于: select * from user where age = 22;
![0](http://dev-img.mos.moduyun.com/20231215/afc805d0-f834-4316-bd39-d146814ada99.png)
![0](http://dev-img.mos.moduyun.com/20231215/d9a9873d-5efe-4d72-9c05-eed91cd8f0ba.png)
-- 查询 age > 22 的记录
db.mongodb_test.find({"age":{$gt:17}}) // select * from user where age >22;
![0](http://dev-img.mos.moduyun.com/20231215/c5b2426d-7b00-4969-ac36-319275fa30be.png)
![0](http://dev-img.mos.moduyun.com/20231215/d8d8f238-d58e-4225-9f48-3e28eb836de8.png)
-- 查询 age < 18 的记录
db.mongodb_test.find({"age":{$lt:18}}) // select * from user where age <18;
![0](http://dev-img.mos.moduyun.com/20231215/2f219eac-5a3d-4941-8524-71d91c39eb58.png)
-- 查询 age >= 25 的记录
-- 查询 age >= 18 的记录
db.mongodb_test.find({"age":{$gte:18}}) //select * from user where age >= 25;
![0](http://dev-img.mos.moduyun.com/20231215/f0b5ed06-c1da-411a-8bde-80888afadea1.png)
![0](http://dev-img.mos.moduyun.com/20231215/712b751f-7e81-42c5-a628-33e9019694da.png)
-- 查询 name 中包含 string 的数据 模糊查询用于搜索
-- 查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.mongodb_test.find({"name":/string/}) //select * from user where name like ‘%string%’;
![0](http://dev-img.mos.moduyun.com/20231215/d6dc63f3-2bef-4f15-b72e-02217fb637e0.png)
![0](http://dev-img.mos.moduyun.com/20231215/172c4842-3765-4db2-829a-eaa1aed79840.png)
-- 查询 name 中以 ad 开头的
db.mongodb_test.find({"name":/^ad/}) //select * from user where name like ‘mongo%’;
![0](http://dev-img.mos.moduyun.com/20231215/f3bea867-f184-467c-8b4a-f87a52a57036.png)
![0](http://dev-img.mos.moduyun.com/20231215/d57a5675-e45a-4725-9300-e3a5f731279e.png)
-- 查询 name 中以 ing 结尾的
db.mongodb_test.find({"name":/ing$/}) //SELECT * FROM car WHERE car.type like '%ing'
![0](http://dev-img.mos.moduyun.com/20231215/ad74f936-5905-430c-a5e8-d6b51797d783.png)
![0](http://dev-img.mos.moduyun.com/20231215/3f71ece7-2e63-4c9b-b4a8-3137401283b5.png)
-- 查询指定列 name、age 数据
name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,
如果用 false 就是排除 name,显示 name 以外的列信息。
db.mongodb_test.find( //select name, age from user;
{},
{"name":1,"age":1}
)
![0](http://dev-img.mos.moduyun.com/20231215/3f65e808-1b24-40c4-8228-f6f80e55328d.png)
![0](http://dev-img.mos.moduyun.com/20231215/eb9d64eb-0aee-446b-91d2-0dab9d7e864f.png)
--查询指定列 name、age 数据, age > 25
db.mongodb_test.find( //select name, age from user where age >25;
{"age":{$gt:17}},
{"name":1,"age":1}
)
![0](http://dev-img.mos.moduyun.com/20231215/c1a65ee9-003d-4d07-b0e7-46b7a4848276.png)
![0](http://dev-img.mos.moduyun.com/20231215/0327063b-6349-4c0a-ac77-86879190149f.png)
-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":1})
![0](http://dev-img.mos.moduyun.com/20231215/5e6161af-6d6b-4793-aff5-9485d618fda6.png)
![0](http://dev-img.mos.moduyun.com/20231215/957280d1-4e21-48c5-90ca-4378515a7d42.png)
-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":-1})
![0](http://dev-img.mos.moduyun.com/20231215/1d4bc4eb-63e0-463e-955a-70b2c07b451b.png)
![0](http://dev-img.mos.moduyun.com/20231215/a7b80f3f-33f1-4a16-95a9-43b16cbaee90.png)
-- 查询 name = String, age = 18 的数据
db.mongodb_test.find({"name":"string","age":18}) //select * from user where name = ‘String’ and age = ‘18’;
![0](http://dev-img.mos.moduyun.com/20231215/d596ae7d-ce67-45ed-b8b8-a1c2f19bff3b.png)
![0](http://dev-img.mos.moduyun.com/20231215/28139f7f-236a-4e10-94ad-b24b44a91067.png)
-- 查询 2 条以后的数据
db.mongodb_test.find().skip(2)
![0](http://dev-img.mos.moduyun.com/20231215/1cc46a2d-487a-4efd-a402-160c6532d135.png)
![0](http://dev-img.mos.moduyun.com/20231215/3f29d6cc-38d4-487f-87b7-8584fa20e2a0.png)
-- 查询在 1-3 之间的数据
可用于分页,limit 是 pageSize,skip 是 (page-1) * pageSize
db.mongodb_test.find().limit(1).skip(3)
![0](http://dev-img.mos.moduyun.com/20231215/05af969c-cf35-48c7-a3d9-bd1f7e56234c.png)
![0](http://dev-img.mos.moduyun.com/20231215/ac59130a-472e-46ce-a8ec-44739fe0c4cc.png)
-- or 或 查询
db.mongodb_test.find( //select * from user where age = 22 or age = 25;
{$or:[
{"age":17},
{"age":16}
]}
)
![0](http://dev-img.mos.moduyun.com/20231215/2bad27f8-d190-4dff-9839-d1b6dfb3c666.png)
![0](http://dev-img.mos.moduyun.com/20231215/db09f2c1-5433-412d-a43c-71a960f4be95.png)
-- findOne 查询第一条数据
db.mongodb_test.findOne() // selecttop 1 * from user; db.user.find().limit(1);
![0](http://dev-img.mos.moduyun.com/20231215/f990ee97-7bca-4328-bda0-606d69ed4cf1.png)
![0](http://dev-img.mos.moduyun.com/20231215/220e18fb-dfce-451e-ab7e-c4fcb0c8b862.png)
-- 查询某个结果集的记录条数 统计数量
db.mongodb_test.find({"age":{$gte:17}}).count()
![0](http://dev-img.mos.moduyun.com/20231215/77504302-bf22-4a0d-b45a-d7bba36c1b18.png)
![0](http://dev-img.mos.moduyun.com/20231215/1491a687-35a2-471b-aa17-0074ff933a5f.png)
修改数据
--修改里面还有查询条件。你要该谁,要告诉 mongo。-- 查找名字叫做小明的,把年龄更改为 16 岁:
db.mongodb_test.update({"name":"string"},{$set:{"sex":"男"}})
![0](http://dev-img.mos.moduyun.com/20231215/6b7f0bac-d092-4df1-9ab7-a3c372abce60.png)
![0](http://dev-img.mos.moduyun.com/20231215/1a9244a5-9ef8-43c4-a006-1bbb0e73900f.png)
-- 查找名字叫做string的,把年龄更改为 22 岁:
db.mongodb_test.update({"name":"string"},{$set:{"age":22}})
![0](http://dev-img.mos.moduyun.com/20231215/5100779a-9106-4897-bbfb-16376978d4f2.png)
![0](http://dev-img.mos.moduyun.com/20231215/4998c9ac-0b1c-4045-bc54-ef05838d52a8.png)
-- 查找数学年龄是 22,把年龄更改为 33 岁 性别改为女:
db.mongodb_test.update({"age":22},{$set:{"age":33,"sex":"女"}});
![0](http://dev-img.mos.moduyun.com/20231215/cd361d57-77ee-434a-9be8-f102c0445710.png)
![0](http://dev-img.mos.moduyun.com/20231215/eff76951-784c-4fa1-a1dd-6c6d0bdd4f5d.png)
-- $inc修改器
-- 比如我们做一个在线用户状态记录,每次修改会在原有的基础上自增$inc指定的值,如果“文档”中没有此key,则会创建key
db.mongodb_test.update({"name":"string"},{$inc:{"age":50}},false,true) //:update users set age = age + 50 where name = ‘Lisi’;
![0](http://dev-img.mos.moduyun.com/20231215/731f2ea1-77dc-4d33-b87d-48b33150f4d8.png)
![0](http://dev-img.mos.moduyun.com/20231215/183f5492-6360-47e3-aec0-29c2f880d4b7.png)
![0](http://dev-img.mos.moduyun.com/20231215/c01c6131-767c-4e44-afb6-a2d1843b36cc.png)
-- 注意:不写 $set 关键字,则完整替换整条数据
db.mongodb_test.update({"name":"string"},{"name":"stringOne"})
![0](http://dev-img.mos.moduyun.com/20231215/24aef708-b4b3-4e79-948a-bfa67b30905e.png)
![0](http://dev-img.mos.moduyun.com/20231215/aa01edf5-eda4-4821-bf35-6e4a6510d09f.png)
![0](http://dev-img.mos.moduyun.com/20231215/85b36e6c-3416-424d-9d2a-0d0000812974.png)
删除
-- 删除指定的数据
db.collectionsNames.remove({"key":"val"})
eg:
db.mongodb_test.remove({"age":{$gt:11}})
![0](http://dev-img.mos.moduyun.com/20231215/e5fc72ff-dd13-4a61-9247-1295c89e88bf.png)
![0](http://dev-img.mos.moduyun.com/20231215/e8a25780-f42e-4bf7-971a-13333205ccac.png)
![0](http://dev-img.mos.moduyun.com/20231215/03fd81e4-fbc6-4621-98c6-6b548ff86f16.png)
-- 默认情况下,remove()方法会删除所有符合 remove 条件的文档。
使用 justOne 选项将删除操作限制为仅对一个匹配文档执行。
db.mongodb_test.remove({"age":{$gt:11}},{justOne:true})
![0](http://dev-img.mos.moduyun.com/20231215/7399c2af-9b19-41b8-8311-8f40b53579a9.png)
![0](http://dev-img.mos.moduyun.com/20231215/5f5b0aac-c41c-445a-b359-7bab6e9f1faf.png)
![0](http://dev-img.mos.moduyun.com/20231215/5e652440-d4dc-4101-8923-dc052fa19f69.png)
到这里这篇也就结束啦!