Navicat 操作MongoDB篇(基本增删改查
创建集合
新建集合
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ohxrgsmn_hqdm.png)
点击保存
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/avujvikp_sv0k.png)
给集合起名字
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ixfzykkz_627n.png)
创建一个查询
新增
-- 新增数据2种方式
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/zmynzeie_m1x0.png)
db.mongodb_test.save({"name":"stringBoot"})
db.mongodb_test.insert({"name":"mango good"});
db.mongodb_test.save({"name":"mango good",type:"工具书"});
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/qdldavut_fuvc.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;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/qhntyxgd_djzn.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/sqbhsabi_v4vo.png)
-- 查询去掉后的当前聚集集合中的某列的重复数据
db.mongodb_test.distinct("name") //会过滤掉 name 中的相同数据 相当于:select distict name from user;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/lcvlfgzs_y2jy.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ghgtbxfd_huz3.png)
-- 查询 age = 17 的记录
db.mongodb_test.find({"age":17}) //相当于: select * from user where age = 22;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/szojuyqr_vbq2.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/xcrjpder_o9ul.png)
-- 查询 age > 22 的记录
db.mongodb_test.find({"age":{$gt:17}}) // select * from user where age >22;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/wdzobsmx_1b0e.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/uklufgbf_y88n.png)
-- 查询 age < 18 的记录
db.mongodb_test.find({"age":{$lt:18}}) // select * from user where age <18;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/suizmbas_2dnb.png)
-- 查询 age >= 25 的记录
-- 查询 age >= 18 的记录
db.mongodb_test.find({"age":{$gte:18}}) //select * from user where age >= 25;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/yefrftlb_v0v1.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/sbnnvyir_ao1s.png)
-- 查询 name 中包含 string 的数据 模糊查询用于搜索
-- 查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.mongodb_test.find({"name":/string/}) //select * from user where name like ‘%string%’;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/krlfmqit_jzv3.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/hvkwjsxb_aawl.png)
-- 查询 name 中以 ad 开头的
db.mongodb_test.find({"name":/^ad/}) //select * from user where name like ‘mongo%’;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ljcfhgwc_lev1.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/yukmvhtg_dd8l.png)
-- 查询 name 中以 ing 结尾的
db.mongodb_test.find({"name":/ing$/}) //SELECT * FROM car WHERE car.type like '%ing'
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/pwdopdse_nz31.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/lxidyacp_2bf5.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}
)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/usgmderg_ahut.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/hyglesto_snqo.png)
--查询指定列 name、age 数据, age > 25
db.mongodb_test.find( //select name, age from user where age >25;
{"age":{$gt:17}},
{"name":1,"age":1}
)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/zuxqzlkc_p075.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/kmvftvam_irre.png)
-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":1})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/woghohbl_3kxu.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/lhythlyq_pj5p.png)
-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":-1})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/hlsrbquu_xmkk.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/wdksjhjb_w6od.png)
-- 查询 name = String, age = 18 的数据
db.mongodb_test.find({"name":"string","age":18}) //select * from user where name = ‘String’ and age = ‘18’;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/yprikgry_5y5s.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/zpnhwbcz_32z2.png)
-- 查询 2 条以后的数据
db.mongodb_test.find().skip(2)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/pofiwiky_f1q2.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/skhtvngn_7s2p.png)
-- 查询在 1-3 之间的数据
可用于分页,limit 是 pageSize,skip 是 (page-1) * pageSize
db.mongodb_test.find().limit(1).skip(3)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/tmhozliq_z2oo.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/sdpwmhkw_pzgu.png)
-- or 或 查询
db.mongodb_test.find( //select * from user where age = 22 or age = 25;
{$or:[
{"age":17},
{"age":16}
]}
)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/hvdmwrti_d7ti.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/cgotmkim_l2kf.png)
-- findOne 查询第一条数据
db.mongodb_test.findOne() // selecttop 1 * from user; db.user.find().limit(1);
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ucbuzcmf_xmid.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/irvopolo_7hy4.png)
-- 查询某个结果集的记录条数 统计数量
db.mongodb_test.find({"age":{$gte:17}}).count()
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/pvqtojuw_ynkg.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/iegelomv_uwqb.png)
修改数据
--修改里面还有查询条件。你要该谁,要告诉 mongo。-- 查找名字叫做小明的,把年龄更改为 16 岁:
db.mongodb_test.update({"name":"string"},{$set:{"sex":"男"}})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/wpjaydyo_ez8e.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/udarixcs_5xr1.png)
-- 查找名字叫做string的,把年龄更改为 22 岁:
db.mongodb_test.update({"name":"string"},{$set:{"age":22}})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ilhnlgph_x2r6.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/gqqnkfhx_rywj.png)
-- 查找数学年龄是 22,把年龄更改为 33 岁 性别改为女:
db.mongodb_test.update({"age":22},{$set:{"age":33,"sex":"女"}});
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/pphaalyh_h0pb.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/hhneapwc_kllt.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’;
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ngvbszqr_42op.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/uhtauexi_tn6h.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/nracdmph_2x41.png)
-- 注意:不写 $set 关键字,则完整替换整条数据
db.mongodb_test.update({"name":"string"},{"name":"stringOne"})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/tablecuv_fmql.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/bmgnietn_8wgz.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/nymxmsff_1y3g.png)
删除
-- 删除指定的数据
db.collectionsNames.remove({"key":"val"})
eg:
db.mongodb_test.remove({"age":{$gt:11}})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/kokpbvdf_inf1.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/nddyffwl_u1qu.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/orswbqum_ebd6.png)
-- 默认情况下,remove()方法会删除所有符合 remove 条件的文档。
使用 justOne 选项将删除操作限制为仅对一个匹配文档执行。
db.mongodb_test.remove({"age":{$gt:11}},{justOne:true})
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/nafbjkrq_f3ub.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/aoncfcht_s2u4.png)
![](https://img.shuduke.com/static_img/cnblogs/blog/2899278/202312/ltabpuwh_kmj2.png)
到这里这篇也就结束啦!
热门相关:一等狂后:绝色驭兽师 扑倒老公大人 诛天至极 写给鼹鼠先生的情书 我有一张均富卡