MongoDB-SQL语法

可视化软件:Robo3t

1. MongoDB-查询

db.getCollection('表名').find({});

db.getCollection('表名').find({"_id":1});

2. MongoDB-NE(NOT EQUAL)查询

db.getCollection('表名').find({"_id":{$ne:1}})

3. MongoDB-IN查询

db.getCollection('表名').find({"_id":{$in:[1,2,3]}});

4. MongoDB-NOT_IN查询

db.getCollection('表名').find({"_id":{$nin:[1,2,3]}})

5. MongoDB-EXISTS查询

db.getCollection('表名').find({"字段名":{$exists:true}});

当boolean为true,$exists匹配包含字段的文档,包括字段值为null的文档。

当boolean为false,$exists返回不包含对应字段的文档。

6. MongoDB-大于小于查询

db.getCollection('表名').find({"_id":{$gt:1}})
  • (>)---大于---$gt
  • (<)---小于---$lt
  • (>=)---大于等于---$gte
  • (<=)---小于等于---$lte

7. MongoDB-大于小于日期查询

  • Date方式
db.getCollection('表名').find({"时间字段":{$gte:new Date(2000,1,1)},"时间字段":{$lte:new Date(2030,1,1)}});
  • ISODate方式
db.getCollection('dictValue').find({"createTime":{$gte:ISODate("2000-01-01T00:00:00Z")},"createTime":{$lte:ISODate("2030-01-01T00:00:00Z")}});

8. MongoDB-模糊匹配

db.表名.find({"字段名": {$regex:'要模糊匹配的字符',$options:'i'}});

$options: '<options>':选项    含义
i===>大小写不敏感
m===>查询匹配中使用了锚,例如^(代表开头)和$(代表结尾),以及匹配n后的字符串
x===>忽视所有空白字符
s===>允许点字符(.)匹配所有的字符,包括换行符

9. MongoDB-修改-所有匹配的数据

db.getCollection('表名').update({"字段名" : "原字段值"},{$set:{"字段名" : "新字段值"}},{multi:true});

10. MongoDB-新增数据

db.getCollection('表名').save({"_id":NumberLong(1)});

11. MongoDB-删除数据

db.getCollection('表名').remove({});
db.getCollection('表名').remove({"_id":1});

12. MongoDB-两个字段比较

常规查询:
db.getCollection('表名').find({$expr:{$gt:["$approve_create", "$approve_delete"]}})
聚合查询:
db.getCollection('表名').aggregate({$match:{$expr:{$gt:["$approve_create", "$approve_delete"]}}})

$gt -------- greater than  >
$gte --------- gt equal  >=
$lt -------- less than  <
$lte --------- lt equal  <=
$ne ----------- not equal  !=
$eq  --------  equal  =

 13.MongoDB-加索引

db.表名.ensureIndex({"字段1":1,"字段2":1,"字段3":1})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值