MongoDB上的CRUD

背景

CRUD 操作是 create(创建), read(读取), update(更新)和 delete(删除) 文档

原文https://www.cnblogs.com/keme/p/11004955.html
link

帮助命令

show dbs 打印服务器上所有数据库的列表。
use <db> 将当前数据库切换到<db>。该 mongoshell 变量 db 被设置为当前数据库。
show collections 打印当前数据库的所有集合的列表。
show users 打印当前数据库的用户列表。
show roles 打印用于当前数据库的用户定义和内置的所有角色的列表。
show databases 打印所有可用数据库的列表。

insert

查看结果
db.test.find()
单条插入
db.test.insert( {
   
   "name":"keme","age":18,"ad":"北京市朝阳区"} )
多行插入
db.inventory.insertMany( [
{ "item": "journal", "qty": 25, "size": { "h": 14, "w": 21, "uom": "cm" }, "status": "A" },
{ "item": "notebook", "qty": 50, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "A" },
{ "item": "paper", "qty": 100, "size": { "h": 8.5, "w": 11, "uom": "in" }, "status": "D" },
{ "item": "planner", "qty": 75, "size": { "h": 22.85, "w": 30, "uom": "cm" }, "status": "D" },
{ "item": "postcard", "qty": 45, "size": { "h": 10, "w": 15.25, "uom": "cm" }, "status": "A" }
]);

select

比较运算符
$eq 匹配等于指定值的值。

$gt 匹配大于指定值的值。

$gte 匹配大于或等于指定值的值。

$in 匹配数组中指定的任何值。

$lt 匹配小于指定值的值。

$lte 匹配小于或等于指定值的值。

$ne 匹配不等于指定值的所有值。

$nin不匹配数组中指定的值。

逻辑查询运算符
$and 使用逻辑连接查询子句AND返回与两个子句的条件相匹配的所有文档。

$not 反转查询表达式的效果,并返回与查询表达式不匹配的文档。

$nor 使用逻辑连接查询子句NOR返回所有无法匹配两个子句的文档。

$or 使用逻辑连接查询子句OR返回与任一子句的条件相匹配的所有文档
db.<collection>.findOne()
db.inventory.find( { status: "D" } )
相当于SQL语句的 
select * from inventory where status="D";

and
db.inventory.find( { status: "A", qty: { $lt: 30 } } )
相当于sql 语句
select * from inventory where status="A" and qty< 30;

or 
db.inventory.find( { $or: [ { status: "A" }, { qty: { $lt: 30 } } ] } )
相当于sql 语句:
select * from inventory where status=A or qty<30;

正则
db.inventory.find( {
   
   status: "A",$or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]} )
相当于sql语句
SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")


```sql
## drop
```sql
db.collection.remove() 删除单个文档或与指定过滤器匹配的所有文档。 参数{},清空集合
db.collection.drop()此方法获取受影响的数据库上的写入锁定,并将阻止其他操作,直到
其完成。 不加参数删除所有数据包括索引
db.collection.deleteOne() 最多删除与指定过滤器匹配的单个文档,即使多个文档可能与指定的过滤器匹配。
db.collection.deleteMany() 删除与指定过滤器匹配的所有文档。

补充

来自互联网
糊查询:
1.%xx%
   sql:
       select * from user where name like "%花%";
   mongo:
       db.user.find(name://);
2.xx%
   sql:
      select * from user where name like "花%";
   mongo:
       db.user.find(name:/^/);
3.不区分大小写
       db.user.find(name:/a/i);


MongoDB 日期查询目前可通过Date 和ISODate两种方式:
MongoDB条件对应关系
 
(>) 大于 - $gt
(<) 小于 - $lt
(>=) 大于等于 - $gte
(<= ) 小于等于 - $lte

1.Date方式
例如查询ct>=2012.12.7 且et<=2012.12.7:可翻译为
"ct":{$gte:new Date(2012,11,7)},"et":{$lte:new Date(2012,11,7)}

如下是查询日期大于等于2016121日的记录条数(注意,中间的月份写11,就是12月)

db.xxx.find({
   
   "ct":{$gte:new Date(2016,11,1)}})

2.ISODate方式(推荐方式)
ISODate("2016-01-01T00:00:00Z")

准确的时间点:{
   
   "CreateDate":{$gte:ISODate("2020-11-16T00:00:00Z")}

例句:CreateDate时间大于1116;GroupType=2;
ChatGroupId字段以  US开头
Owner字段  不以 US开头

db.getCollection('ChatGroup').find({
   
   "CreateDate":{$gte:ISODate("2020-11-16T00:00:00Z")},"GroupType":2,"ChatGroupId":/^US/,"Owner":{$not:/^US/}})


3.示列
以ISODate查询:
 
db.xxxx.find({
   
   "ct":{
   
   "$gt":ISODate("2017-04-20T01:16:33.303Z")}}) // 大于某个时间
db.xxxx.find({
   
   "ct":{
   
   "$lt":ISODate("2017-04-20T01:16:33.303Z")}}) // 小于某个时间
db.xxxx.find({
   
   "$and":[{
   
   "ct":{
   
   "$gt":ISODate("2017-04-20T01:16:33.303Z")}},{
   
   "ct":{
   
   "$lt":ISODate("2018-12-05T01:16:33.303Z")}}]}) // 某个时间段
db.xxxx.find({
   
   "ct":{
   
   "$gte":ISODate("2017-04-20T01:16:33.303Z"),"$lte":ISODate("2018-12-05T01:16:33.303Z")}}) //某个时间段


补充:
查找
db.getCollection('ChatGroup').find({
   
   "GroupId":"2921***553"})


排序:
.sort({
   
   "CreateDate":-1})

全更新:{
   
   "GroupId":"2921***53"}为更新条件
db.ChatGroup.update({
   
   "GroupId":"2921870553"},{$set:{
   
   "Owner":"US120*****5191-00DD"}},false,true)

本文说明,主要技术内容来自互联网技术大佬的分享,还有一些自我的加工(仅仅起到注释说明的作用)。如有相关疑问,请留言,将确认之后,执行侵权必删

猜你喜欢

转载自blog.csdn.net/baidu_34007305/article/details/110237146