MongoDB学习-->Gridfs分布式存储&DBRef关联查询
mongodb自带的一个分布式文件系统
fs.files _id filename md5 size uploaddate contenttype metadata {"user_id":1}
fs.chunks _id files_id n(序号) data
{ "metadata" : { "user_id" : 101} , "filename" : "video.min2.js" , "aliases" : null , "chunkSize" : 261120 , "uploadDate" : { "$date" : "2017-03-28T13:48:20.784Z"} , "length" : 282753 , "_id" : { "$oid" : "58da69a45aa6c70234eb70f1"} , "contentType" : null , "md5" : "e5ab0872d9b25fcb7268df3a93c29873"}
测试GridFs文件上传与下载
package com.tangzhe.gridfs; import com.mongodb.BasicDBObject;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.test.context.junit4.SpringRunner; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; /**
* Created by 唐哲
* 2018-03-15 9:33
* 测试GridFs文件上传与下载
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GridFsTest { @Autowired
private GridFsTemplate gridFsTemplate; /**
* 测试GridFs文件上传
*/
//@Test
public void gridFsUpload() throws FileNotFoundException {
//System.out.println(gridFsTemplate);
File file = new File("D:\\IdeaProjects\\cxytiandi\\mongodb-demo\\upload.txt");
GridFSFile gridFSFile = gridFsTemplate.store(new FileInputStream(file), file.getName(), new BasicDBObject("user_id", 101));
System.out.println(gridFSFile.toString());
} /**
* 测试GridFs文件下载
*/
//@Test
public void gridFsDownload() throws IOException {
GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is("5aa9cf31d678412b40ff5ab4")));
gridFSDBFile.writeTo("D:\\IdeaProjects\\cxytiandi\\mongodb-demo\\download.txt");
} }
DBRef关联查询
package com.tangzhe.dbref; import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; /**
* Created by 唐哲
* 2018-03-15 16:19
* DBRef关联查询
*/
@Document(collection = "class_info")
@Data
public class ClassInfo { @Id
private String id;
private String name; }
package com.tangzhe.dbref; import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document; /**
* Created by 唐哲
* 2018-03-15 16:29
* DBRef关联查询
*/
@Document(collection = "student")
@Data
public class Student { @Id
private String id;
private String name;
@DBRef
private ClassInfo classInfo; }
package com.tangzhe.dbref; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner; /**
* Created by 唐哲
* 2018-03-15 16:30
* 测试DBRef关联查询
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DbRefTest { @Autowired
private MongoTemplate mongoTemplate; //@Test
public void test1() {
ClassInfo classInfo = new ClassInfo();
classInfo.setName("三年一班");
mongoTemplate.save(classInfo); Student student = new Student();
student.setName("张三");
student.setClassInfo(classInfo);
mongoTemplate.save(student);
} //@Test
public void test2() {
ClassInfo classInfo = mongoTemplate.findOne(Query.query(Criteria.where("id").is("5aaa30c0d678410bac20db1a")), ClassInfo.class);
Student student = new Student();
student.setName("李四");
student.setClassInfo(classInfo);
mongoTemplate.save(student);
} //@Test
public void test3() {
Student student = mongoTemplate.findOne(Query.query(Criteria.where("id").is("5aaa30c0d678410bac20db1b")), Student.class);
System.out.println(student);
} }
MongoDB学习-->Gridfs分布式存储&DBRef关联查询的更多相关文章
- ofbiz学习笔记01--多表关联查询
不管做什么项目,肯定会用到多表关联查询数据,从网络查询得知ofbiz有三种多表关联查询方法 实现一:Screem.xml 中的 section 里,加 <action>, 加 get-re ...
- Mybatis框架学习总结-表的关联查询
一对一关联 创建表和数据:创建一张教师表和班级表,这里假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关系. CREATE TABLE teacher( t_id INT PRIM ...
- Mybatis学习总结四(关联查询)
一.一对一查询 实例:查询所有订单信息,关联查询下单用户信息. Method1:使用resultType,定义订单信息po类,此po类中包括了订单信息和用户信息. public class Order ...
- 详解MongoDB中的多表关联查询($lookup)
一. 聚合框架 聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息. 聚合管道操作主要包含下面几个部分: 命令 功能描述 $projec ...
- 详解MongoDB中的多表关联查询($lookup) (转)
一. 聚合框架 聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息. 聚合管道操作主要包含下面几个部分: 命令 功能描述 $projec ...
- [Spring Data MongoDB]学习笔记--MongoTemplate查询操作
查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery q ...
- mongoDB学习笔记——在C#中查询
1.下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csh ...
- MongoDB 学习笔记之 DBRef
DBRef: MongoDB建模有两种方式,一种是内嵌(Embed),另一种是连接(Link).内嵌比较好理解,就是字段内容是个数组,数组内再包含文档,而我们今天介绍的是另一种,称为链接DBRef.由 ...
- MongoDB 集合间关联查询后通过$filter进行筛选
在前面的分享中,有讲解 “详解MongoDB中的多表关联查询($lookup)” 一节,其内容涵盖了常见的集合管理的需求.我们知道文档的选择都是通过$match进行匹配刷选.但这是文档间的匹配筛选,并 ...
随机推荐
- VS2013使用EF6通过ADO.NET 连接mySql成功步骤
VS2013使用EF6通过ADO.NET 连接mySql成功步骤 1.安装mysql-for-visualstudio-1.2.6(我用的目前最新版,这个一般安装VS2013就已经有了,没有的话下载一 ...
- spring项目后台中打印sql语句
配置方法很简单,只要在配置文件application.yml加入以下语句 logging: level: main.java.iReview.dao: debug 其中包的位置应为dao层的包位置,不 ...
- Spring 基础知识 - 依赖注入
所谓的依赖注入是指容器负责创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖. 依赖注入主要目的是为了解耦,体现了一种“组合”的理念. 无论是xml配置.注解配置还是Ja ...
- H5的storage(sessionstorage&localStorage)简单存储删除
众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...
- STM8 PIN setting(output)
今日在设置引脚输出的时候,本想设置为open-drain输出,然后对其输出高低.但是发现无法输出高(初始化为开漏低电平),始终为低.后来改为push-pull 输出,就能输出高低了.真有意思,转到SP ...
- 字符串(String)杂谈
作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com >>>转载请注明出处!<<< 上一次我们已经一起回顾了面试题 ...
- jquery.restrictFieldLength.js
1.参考资料 http://www.cnblogs.com/aarond/archive/2013/08/02/3234042.html 2.使用举例 //字符控制 $(function () { $ ...
- python爬虫之路——初识函数与控制语句
介绍python函数的定义与使用方法,介绍Python的常用控制语句:判断和循环 函数: 格式 def 函数名(参数1,参数2): return ‘结果’ 判断语句:就是多选一 二选一: if c ...
- hdu 5093 Battle ships (二分图)
二分图最大匹配问题 遇到冰山就把行列拆成两个部分.每个部分x也好,y也好只能匹配一次 图画得比较草,将就着看 横着扫一遍,竖着扫一遍,得到编号 一个位置就对应一个(xi,yi)就是X集到Y集的一条边, ...
- UITableView设计思想 考察
整体使用了build模式:单是组织结构混乱:不符合人类思维. UITableViewDataSource:描述了View的要素个数情况:并担负了builder功能. UITableViewDelega ...