使用robomongo工具
1.sort
- 对某个字段进行排序:
- db.getCollection('content').find({}).sort({"time":-1})
- -1:代表倒叙;
- 1:代表正序;
- 对某个字段的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;
- db.getCollection('content').find({"times":{$type:"long"}})
- db.getCollection('content').find({"timestamp":{$exists:true}})
- true:存在
- false:不存在
- 将users这个字段删除
- db.getCollection('content').update({},{"$unset":{'users':1}},false,true)
- 将’abc‘字段名改为’def‘
- db.test.update({}, {$rename : {"abc" : "def"}}, false, true)
- 语法:
{$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }