<shell>MongoDB神奇命令,常用常用非常实用!

使用robomongo工具
1.sort

  •   对某个字段进行排序:
  •   db.getCollection('content').find({}).sort({"time":-1})
  •    -1:代表倒叙;
  •    1:代表正序;

2.模糊查询

  •   对某个字段的value进行模糊查询:
  •   db.getCollection('content').find({"user":{$regex:'小名''}})
  •   或:
  •   db.getCollection('content').find({"user":/小名/})
  •   $options:"$i" ,不区分大小写
  •   mongodbApi写法如下:
  •   Pattern pattern = Pattern.compile("^.*"  + 名 +  ".*$" ,  Pattern.CASE_INSENSITIVE);  
  •   如果字段名称为name,则查询写法如下:
  •   new BasicDBObject().append("name", pattern);


3、Distinct
  • db.runCommand({distinct:"person",key:"country"}).values;

4.查看某个字段的type为long

  • db.getCollection('content').find({"times":{$type:"long"}})

5.查看存在某个字段的数据

  • db.getCollection('content').find({"timestamp":{$exists:true}})
  •  true:存在
  •  false:不存在

6.删除某个字段

  • 将users这个字段删除
  • db.getCollection('content').update({},{"$unset":{'users':1}},false,true)

7.字段重命名

  • 将’abc‘字段名改为’def‘
  • db.test.update({}, {$rename : {"abc" : "def"}}, false, true)
  • 语法:
    {$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }
   更新数据:   

   db.getCollection('user').update({},{$set:{"age":14}},false,true)

   更新多条数据用“,”隔开。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值