您的位置:首页 > 数据库 > Mongodb

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

2017-03-07 19:14 701 查看

使用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)

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb shell