Navicat 操作MongoDB篇(基本增删改查
创建集合
新建集合

点击保存

给集合起名字

创建一个查询
新增
-- 新增数据2种方式

db.mongodb_test.save({"name":"stringBoot"})
db.mongodb_test.insert({"name":"mango good"});
db.mongodb_test.save({"name":"mango good",type:"工具书"});

-- 新增多条数据
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;


-- 查询去掉后的当前聚集集合中的某列的重复数据
db.mongodb_test.distinct("name") //会过滤掉 name 中的相同数据 相当于:select distict name from user;


-- 查询 age = 17 的记录
db.mongodb_test.find({"age":17}) //相当于: select * from user where age = 22;


-- 查询 age > 22 的记录
db.mongodb_test.find({"age":{$gt:17}}) // select * from user where age >22;


-- 查询 age < 18 的记录
db.mongodb_test.find({"age":{$lt:18}}) // select * from user where age <18;

-- 查询 age >= 25 的记录
-- 查询 age >= 18 的记录
db.mongodb_test.find({"age":{$gte:18}}) //select * from user where age >= 25;


-- 查询 name 中包含 string 的数据 模糊查询用于搜索
-- 查询 name 中包含 mongo 的数据 模糊查询用于搜索
db.mongodb_test.find({"name":/string/}) //select * from user where name like ‘%string%’;


-- 查询 name 中以 ad 开头的
db.mongodb_test.find({"name":/^ad/}) //select * from user where name like ‘mongo%’;


-- 查询 name 中以 ing 结尾的
db.mongodb_test.find({"name":/ing$/}) //SELECT * FROM car WHERE car.type like '%ing'


-- 查询指定列 name、age 数据
name 也可以用 true 或 false,当用 ture 的情况下和 name:1 效果一样,
如果用 false 就是排除 name,显示 name 以外的列信息。
db.mongodb_test.find( //select name, age from user;
{},
{"name":1,"age":1}
)


--查询指定列 name、age 数据, age > 25
db.mongodb_test.find( //select name, age from user where age >25;
{"age":{$gt:17}},
{"name":1,"age":1}
)


-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":1})


-- 按照年龄排序 1 升序 -1 降序
db.mongodb_test.find().sort({"age":-1})


-- 查询 name = String, age = 18 的数据
db.mongodb_test.find({"name":"string","age":18}) //select * from user where name = ‘String’ and age = ‘18’;


-- 查询 2 条以后的数据
db.mongodb_test.find().skip(2)


-- 查询在 1-3 之间的数据
可用于分页,limit 是 pageSize,skip 是 (page-1) * pageSize
db.mongodb_test.find().limit(1).skip(3)


-- or 或 查询
db.mongodb_test.find( //select * from user where age = 22 or age = 25;
{$or:[
{"age":17},
{"age":16}
]}
)


-- findOne 查询第一条数据
db.mongodb_test.findOne() // selecttop 1 * from user; db.user.find().limit(1);


-- 查询某个结果集的记录条数 统计数量
db.mongodb_test.find({"age":{$gte:17}}).count()


修改数据
--修改里面还有查询条件。你要该谁,要告诉 mongo。-- 查找名字叫做小明的,把年龄更改为 16 岁:
db.mongodb_test.update({"name":"string"},{$set:{"sex":"男"}})


-- 查找名字叫做string的,把年龄更改为 22 岁:
db.mongodb_test.update({"name":"string"},{$set:{"age":22}})


-- 查找数学年龄是 22,把年龄更改为 33 岁 性别改为女:
db.mongodb_test.update({"age":22},{$set:{"age":33,"sex":"女"}});


-- $inc修改器
-- 比如我们做一个在线用户状态记录,每次修改会在原有的基础上自增$inc指定的值,如果“文档”中没有此key,则会创建key
db.mongodb_test.update({"name":"string"},{$inc:{"age":50}},false,true) //:update users set age = age + 50 where name = ‘Lisi’;



-- 注意:不写 $set 关键字,则完整替换整条数据
db.mongodb_test.update({"name":"string"},{"name":"stringOne"})



删除
-- 删除指定的数据
db.collectionsNames.remove({"key":"val"})
eg:
db.mongodb_test.remove({"age":{$gt:11}})



-- 默认情况下,remove()方法会删除所有符合 remove 条件的文档。
使用 justOne 选项将删除操作限制为仅对一个匹配文档执行。
db.mongodb_test.remove({"age":{$gt:11}},{justOne:true})



到这里这篇也就结束啦!
热门相关:一等狂后:绝色驭兽师 扑倒老公大人 诛天至极 写给鼹鼠先生的情书 我有一张均富卡