Mongdb常用操作

1、数据库库

//1、创建数据库;如果newdb不存在,则自动创建

 //2、删除数据库
 db.dropDatabase()

 //3、查看所有数据库
 show dbs

2、集合

//1、创建集合
db.createCollection("tesst")

//2、查看所有集合
show collections

//3、删除集合
db.test.drop()

3、索引

//1、后台创建复合索引
db.getCollection('test').createIndex({"age":-1,"name":1},{"background":true})

//2、查看索引
db.getCollection('test').getIndexes()

//3、删除索引[根据索引名称删除]
db.getCollection('test').dropIndex("indexName")

4、查询

//1、根据值和范围查询,并按age倒序
db.getCollection('test').find({"name":"liao","age":{"$gte":10,"$lte":100}}).sort({"age"":-1})

//2、指定返回的字段
db.getCollection('test').find({"name":"liao","age":{"$gte":10,"$lte":100}},{"age":1,"_id":1})

//3、修改返回的结果【其实就是写个js方法遍历下结果】
db.getCollection('test').find({"name":"liao"},{"birth":1,"_id":0}).forEach(function (a) { a["birth"] = (new Date(a.birth*1000).toString()); printjson(a) })

4、聚合

1、查询并分组统计
db.getCollection('test').aggregate([
    {$match:{"birth":{"$gte":1529856000000,"$lt":1529891460000}}},
    {$group:{"_id":"$age","num_tutorial":{$sum : 1}}}
    ])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值