MongoDB基础语法

MongoDB为非关系型数据库,无法联表查询,但可以通过shell代码实现

工具:Robo 3T

以下为常用的语法:

db.getCollection('表名').insert(

{title: 'MongoDB例子',

description: '这是一个插入文档的MongoDB例子',

url: '[http://www.test.com'](http://www.test.com'/);,

tags: ['mongodb', 'database', 'NoSQL'],

likes: 100}

)

db.getCollection('表名').remove({'title':' MongoDB例子'})

db.getCollection('表名').update({'title':'MongoDB 教程'},{$set:{'title':' MongoDB更新后的例子'}})

1、常用查询并排序
db.getCollection('表名').find({"name" : "测试套餐1111"}).sort({"createDate":-1})     
注:1为升序,-1为降序

2、多条件or查询
db.getCollection('UserEntity').find({$or:[{"phone" : "18700000000"},{"phone" : "13400000000"}]})

3、多条件and查询
db.getCollection('UserEntity').find({"phone" : "18700000000","phone" : "13400000000"})

4、模糊查询
db.getCollection('UserEntity').find({"name" : /测试/})

5、查询去重后name列数据
db.getCollection('UserEntity').distinct("name")

6、只查询5条数据
db.getCollection('UserEntity').find().limit(5)

7、查询5-10之间数据    
db.getCollection('UserEntity').find().limit(10).skip(5)

8、查询记录条数
db.getCollection('UserEntity').find().count()

9、分组查询
//单个字段分组查询
db.getCollection('UserEntity').aggregate([{$group : {_id : "$balance", num : {$sum: 1}}}])  

//多个字段分组查询
db.getCollection('UserEntity').aggregate([{$group : {_id : {balance:"$balance",expressInc:"$expressInc"}, num : {$sum : 1}}}])  

条件查询

> $gt , >= $gte, < $lt, <= $lte, != $ne 
db.tianyc02.find({age:{$lt:100,$gt:20}})


$in ,$nin
db.tianyc02.find({age:{$in:[11,22]}})
db.tianyc02.find({age:{$nin:[11,22]}})

注:gt=greater than  lt=less than   ne=not equal

转载地址:https://www.jianshu.com/p/5e870132ca7c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值