学习笔记:MongoDB命令的基础学习,使用springboot实现mongodb视图创建

//创建集合
db.createCollection("my")
//查看集合
show collections
//删除集合
db.my.drop()
//插入一条,没有comment该集合,mongodb自动创建
db.comment.insert({"_id":"10002","articleid":"10000","content":"天气多云","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null})
//查询comment集合的所有数据
db.comment.find()
//插入多条
db.comment.insertMany([
{"_id":"10000","articleid":"10000","content":"天气多云","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null},
{"_id":"10001","articleid":"10001","content":"天气晴天","userid":"1002","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(30),"state":"2"},
{"id":"10002","articleid":"10002","content":"天气阴天","userid":"1003","nickname":"Rose","createdatetime":new Date("2020-12-23 12:00:25"),"likenum":NumberInt(20),"state":"1"}])
//查询所有articleid=10000的数据
db.comment.find({"articleid":"10000"})
//查询第一条articleid=10000的数据
db.comment.findOne({"articleid":"10000"})
//显示articleid列的数据,默认显示_id列的数据
db.comment.find({articleid:"10000"},{articleid:1})
//显示articleid列的数据,不认显示_id列的数据
db.comment.find({articleid:"10000"},{articleid:1,_id:0})
//捕获错误
try{
db.comment.insertMany([
{"_id":"10000","articleid":"10000","content":"天气多云","userid":"1001","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(10),"state":null},
{"_id":"10001","articleid":"10001","content":"天气晴天","userid":"1002","nickname":"Rose","createdatetime":new Date(),"likenum":NumberInt(30),"state":"2"},
{"id":"10003","articleid":"10002","content":"天气阴天","userid":"1003","nickname":"Rose","createdatetime":new Date("2020-12-23 12:00:25"),"likenum":NumberInt(20),"state":"1"}])
}catch(e){
	print(e)
}
db.comment.find()
//全局修改
db.comment.update({_id:"10000"},{likenum:1000})
//局部修改
db.comment.update({_id:"10001"},{$set:{likenum:1000}})
//局部批量修改数据
db.comment.update({userid:"1001"},{$set:{nickname:"jack"}},{multi:true})
//列值增长
db.comment.update({_id:"10001"},{$inc:{likenum.NumberInt(1)}})
//删除符合条件的
db.comment.remove({userid:"1003"})
//删除全部
db.comment.remove({})
//查询数量
db.comment.count()
//查询符合条件的数量
db.comment.count({userid:"1001"})
//查询前两条
db.comment.find().limit(3)
//跳过前两条数据,限制显示几条(可以用作翻页查询)
db.comment.find().skip(1).limit(2)
//按条件排序1为升序,0为降序
db.comment.find().sort({userid:1})
//复杂查询
//比较查询
db.comment.find({likenum:{$gt:NumberInt(70)}})
//正则查询
db.comment.find({content:/多云/})
//包含查询
db.comment.find({userid:{$in:["1002","1003"]}})
//条件连接查询
db.comment.find({$and:[{likenum:{$gt:NumberInt(70)}},{userid:{$in:["1002","1001"]}}]})
db.comment.find({$or:[{likenum:{$gt:NumberInt(70)}},{userid:{$in:["1002","1001"]}}]})
//查看索引
db.comment.getIndexes()
//创建索引
db.comment.createIndex({userid:1})
//删除索引
db.comment.dropIndex({userid:1})
//根据名字删除
db.comment.dropIndex("userid_1")
//删除所有
db.comment.dropIndexes() 

//查看语句的执行计划
db.comment.find({userid:"1001"}).explain()
//涵盖查询
//当条件和查询的投影仅包含索引字段时,mongodb直接从索引返回结果,而不扫描任何文档或将文档带入内存
db.comment.find({likenum:{$gt:NumberInt(70)}},{userid:1,_id:0})

//创建视图
db.createView(
	"union",//视图名称
	"cycle",//主表
	[
	//连接的副表 from副表名称 localField 副表id foreignField主表id as,主表和副表查询数据起的别名接收
		{$lookup:{from:"fetal_condition",localField:"cycle_id",foreignField:"cycle_id",as:"fetalCondition"}},
		//$unwind让数据以文档的形式呈现,没有则是数组形式,includeArrayIndex数组下标的别名 preserveNullAndEmptyArrays:true允许空数组存在
		{$unwind:{path:"$fetalCondition",includeArrayIndex:"index",preserveNullAndEmptyArrays:true}},
		{$lookup:{from:"follow_up",localField:"cycle_id",foreignField:"cycle_id",as:"followUp"}},
		{$unwind:{path:"$followUp",includeArrayIndex:"index",preserveNullAndEmptyArrays:true}},
	]
)
//删除视图
db.union.drop()
//查看视图数据
db.union.find()

mongodb创建视图,使用springboot实现mongodb视图创建
下面创建的视图和命令创建的效果一致。
查询视图数据和查询文档大差不差,如果有更好的方法,欢迎大家交流。
find方法后面可以点出来个过滤方法,我不知道怎么用,请各位指教

   List<Document> documents = mongoTemplate.find(new Query().addCriteria(条件,接收的类, 视图名);
public String createView(){
        String viewName="union";//视图名
        String sourceName="cycle";//主表名字
        LookupOperation lookupOperation1 = LookupOperation.newLookup()
                .from("fetal_condition")
                .localField("cycle_id")//主表的主键
                .foreignField("cycle_id")//副表的主键
                .as("fetalCondition");//接收副表数据起的别名
        LookupOperation lookupOperation2 = LookupOperation.newLookup()
                .from("follow_up")
                .localField("cycle_id")
                .foreignField("cycle_id")
                .as("followUp");
        //Unwind可以把数据拆成文档,接收的数据处理格式:接收副表数据起的别名,数组下标的别名,保留null和空数组
        UnwindOperation unwind1 = UnwindOperation.newUnwind().path("fetalCondition").arrayIndex("index").preserveNullAndEmptyArrays();
        UnwindOperation unwind2 = UnwindOperation.newUnwind().path("followUp").arrayIndex("index").preserveNullAndEmptyArrays();
        //包含所有条件
        Aggregation aggregation = Aggregation.newAggregation( lookupOperation1, unwind1, lookupOperation2, unwind2);
        /*System.out.println(aggregation.toString().indexOf("["));
        System.out.println(aggregation.toString().lastIndexOf("]"));*/
        String str=aggregation.toString().substring(aggregation.toString().indexOf("[")+1,aggregation.toString().lastIndexOf("]"));
        List<Bson> pipeline=new ArrayList<>();
        pipeline.add(Document.parse(str));
        mongoTemplate.getDb().createView(viewName,sourceName , pipeline);
        return this.existsView()? "视图创建成功" :"视图创建失败";
    }
 //删除视图
    public String dropView(){
        mongoTemplate.getCollection("union").drop();
        return this.existsView()? "视图删除失败" :"视图删除成功";
    }
//判断视图是否存在
    public boolean existsView(){
        return mongoTemplate.collectionExists("union");
    }
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐