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进行匹配刷选.但这是文档间的匹配筛选,并 ...
随机推荐
- c# 字符串的首字母大写转换 方法
方法1: s.Substring(0,1).ToUpper()+s.Substring(1); 方法2: s = System.Threading.Thread.CurrentThread.Curr ...
- 在CentOS上源码安装Nginx
总步骤: wget http://nginx.org/download/nginx-1.10.1.tar.gz tar -xvf nginx-1.10.1.tar.gz cd nginx-1.10.1 ...
- 【简记】HTML + CSS 的一些要点(不定时更新)
1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...
- CoordinatorLayout使用笔记
CoordinatorLayout的使用笔记 首先第一个子控件是AppBarLayout存放首部控件,里面放了一个CollapsingToolbarLayout.代码如下: <android.s ...
- UVA 10382 Watering Grass (区间覆盖,贪心)
问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...
- cv2.threshold 阈值灰度
threshold函数的使用 图像的二值化就是将图像上的像素点的灰度值设置为0或255,这样将使整个图像呈现出明显的黑白效果.在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大 ...
- stixel world论文总结
1.The Stixel World - A Compact Medium Level Representation of the 3D-World:http://pdfs.semanticschol ...
- 超全面Java 面试题(2.1)
这部分主要是开源JavaEE框架方面的内容,包括hibernate.MyBatis.spring.Spring MVC等,由于Struts2已经是明日黄花,在这里就不讨论Struts2的面试题,此外, ...
- Ab initio methods|Evidence-based methods|maximum-likelihood|branch-site|H1|H0|GO|dS/dN ratio
(Gene prediction and comparison) 使用基于基因组序列的从头预测方法(Ab initio methods)(同时分别使用头预测软件( GENSCAN和 AUGUSTUS) ...
- bootstrap 警告(Alerts)
本章将讲解警告(Alerts)以及bootstrap所提供的用于警告的class类.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添 ...