1、参考连接
2、使用MongoRepository对多个字段排序
Page<DownloadApplyRecord> findByUserIdOrderByIsReadAscDownloadOverTimeDesc(String userId, Pageable pageable);
//不能出现多个OrderBy,直接拼上多个排序的字段就行。可以试着调换排序字段的前后顺序
3、通过命令导出、导入MongoDB数据
导出
1、 ./mongoexport -d 被导出的数据库名 -c 被导出的集合名 -o 导出后生成的文件名.dat
2、 ./mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -u xx -p xx -d xx -o LL.dat
./mongodump -h 127.0.0.1:27017 --authenticationDatabase admin -d xx -o LL.dat
导入
./mongoimport -h 127.0.0.1:27017 --authenticationDatabase admin -u 用户名 -p 密码 -d 数据库名 -c 集合名 被导入的文件名.dat
4、验证登录MongoDB数据库
5、MongoDB查询操作限制返回字段的方法
//只输出id和title字段,第一个参数为查询条件,空代表查询所有 指定某个字段为1时_id也默认查出来,不现实“_id”:0不显示
db.news.find( {}, { id: 1, title: 1 } )
//如果需要输出的字段比较多,不想要某个字段,可以用排除字段的方法
//不输出内容字段,其它字段都输出
db.news.find( {}, {content: 0 } )
6、删除数据库中的collection
use game //使用game,以下假设操作的collection是foo
db.foo.remove({"id":"bar"}) //删除一条数据
db.foo.remove() //删除foo中的所有记录,但是foo还存在,show collection还可以看到foo
db.foo.drop() //删除foo这个collection,(show collection已经看不到foo了)但是查看数据文件发现大小不变,Mongodb不会自动释放文件空间
db.repairDatabase() //执行这个命令后,Mongodb会把不需要的空间释放出来
参考:https://www.cnblogs.com/gswang/p/7299150.html
7、添加数据库用户
> use flowpp
switched to db flowpp
> db.createUser({user: "flowpp", pwd: "flopww", roles: [{ role: "dbOwner", db: "flowpp" }]}) # 创建用户flowpp,设置密码flopww,设置角色dbOwner
8、修改MongoDB字段类型
#将字段log2FC由string类型修改为Decimal128类型
db.scRNAseq_deg.find(
{"log2FC":{$type:2}}
)
.forEach(function(x){
x.log2FC=NumberDecimal(x.log2FC);
db.scRNAseq_deg.save(x)
}
)
9、模糊查询
#查询包含GSE1999
{"name":/GSE1999/}
#查询以GSE1999开头
{"name":/^GSE1999/}
#查询以88结尾
{"name":/88/}
#查询忽略大小写
{"name":/gse1999/i}
10、新增字段并赋值
#将User表中所有address字段的值为null/没有这个字段的对象,添加一个address字段并且赋值为“ZH”
db.getCollection('user').find({'address' : null}).forEach(
function(item){
db.getCollection('user').update({'address' : null},{$set:{'address' : "ZH"}})
}
)
11、查询分组统计group Java中unwind
//查询subject表中count>1的数据,根据{level2_name}字段进行分组统计
db.getCollection("subject").aggregate(
{$match:{count:{$gte:1}}},
{
"$group":{"_id":"$level2_name",total:{$sum:1}}
}
)
//java unwind拆分数组,分组统计
public void method(){
//根据数组字段organism:["a","b"]分组统计数量
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.unwind("organism"),
Aggregation.group("organism").count().as("total"),
//按照total降序
Aggregation.sort(Sort.Direction.DESC,"total")
);
AggregationResults<Map> aggregate = mongoTemplate.aggregate(aggregation, User.class, Map.class);
List<Map<String, Object>> resultMapList = (List<Map<String, Object>>)aggregate.getRawResults().get("results");
if (CollectionUtils.isNotEmpty(resultMapList)) {
for (Map<String, Object> map : resultMapList) {
System.out.println(map.get("_id"));
System.out.println(map.get("total"));
}
}
System.out.println(JSON.toJSONString(resultMapList, true));
}
12、判断数组[]中是否为空或者有值(参考链接)
方法一:{"field1.0":{$exists: true}} (推荐)
db.testcollection.find({"field1.0":{$exists: true}})
方法二:$elemMatch:{$ne:null}
db.testcollection.find({"field1":{$elemMatch:{$ne:null}}})
方法三:$where:"this.field1.length>0"
db.testcollection.find({$where:"this.field1.length>0"})
方法四:{"field1":{$gt: []}}
db.testcollection.find({"field1":{$gt: []}})
13、值不在集合数组中
#name字段值不是"王二狗","黄大锤","韩梅梅"的数据 值不在数组集合中
{"name" : {$nin:["王二狗","黄大锤","韩梅梅"]}}
14、查询操作符(参考)