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文件上传与下载

 1 package com.tangzhe.gridfs;
 2 
 3 import com.mongodb.BasicDBObject;
 4 import com.mongodb.gridfs.GridFSDBFile;
 5 import com.mongodb.gridfs.GridFSFile;
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.boot.test.context.SpringBootTest;
10 import org.springframework.data.mongodb.core.query.Criteria;
11 import org.springframework.data.mongodb.core.query.Query;
12 import org.springframework.data.mongodb.gridfs.GridFsTemplate;
13 import org.springframework.test.context.junit4.SpringRunner;
14 
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 
20 /**
21  * Created by 唐哲
22  * 2018-03-15 9:33
23  * 测试GridFs文件上传与下载
24  */
25 @RunWith(SpringRunner.class)
26 @SpringBootTest
27 public class GridFsTest {
28 
29     @Autowired
30     private GridFsTemplate gridFsTemplate;
31 
32     /**
33      * 测试GridFs文件上传
34      */
35     //@Test
36     public void gridFsUpload() throws FileNotFoundException {
37         //System.out.println(gridFsTemplate);
38         File file = new File("D:\IdeaProjects\cxytiandi\mongodb-demo\upload.txt");
39         GridFSFile gridFSFile = gridFsTemplate.store(new FileInputStream(file), file.getName(), new BasicDBObject("user_id", 101));
40         System.out.println(gridFSFile.toString());
41     }
42 
43     /**
44      * 测试GridFs文件下载
45      */
46     //@Test
47     public void gridFsDownload() throws IOException {
48         GridFSDBFile gridFSDBFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is("5aa9cf31d678412b40ff5ab4")));
49         gridFSDBFile.writeTo("D:\IdeaProjects\cxytiandi\mongodb-demo\download.txt");
50     }
51 
52 }

DBRef关联查询

 1 package com.tangzhe.dbref;
 2 
 3 import lombok.Data;
 4 import org.springframework.data.annotation.Id;
 5 import org.springframework.data.mongodb.core.mapping.Document;
 6 
 7 /**
 8  * Created by 唐哲
 9  * 2018-03-15 16:19
10  * DBRef关联查询
11  */
12 @Document(collection = "class_info")
13 @Data
14 public class ClassInfo {
15 
16     @Id
17     private String id;
18     private String name;
19 
20 }
 1 package com.tangzhe.dbref;
 2 
 3 import lombok.Data;
 4 import org.springframework.data.annotation.Id;
 5 import org.springframework.data.mongodb.core.mapping.DBRef;
 6 import org.springframework.data.mongodb.core.mapping.Document;
 7 
 8 /**
 9  * Created by 唐哲
10  * 2018-03-15 16:29
11  * DBRef关联查询
12  */
13 @Document(collection = "student")
14 @Data
15 public class Student {
16 
17     @Id
18     private String id;
19     private String name;
20     @DBRef
21     private ClassInfo classInfo;
22 
23 }
 1 package com.tangzhe.dbref;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.data.mongodb.core.MongoTemplate;
 8 import org.springframework.data.mongodb.core.query.Criteria;
 9 import org.springframework.data.mongodb.core.query.Query;
10 import org.springframework.test.context.junit4.SpringRunner;
11 
12 /**
13  * Created by 唐哲
14  * 2018-03-15 16:30
15  * 测试DBRef关联查询
16  */
17 @RunWith(SpringRunner.class)
18 @SpringBootTest
19 public class DbRefTest {
20 
21     @Autowired
22     private MongoTemplate mongoTemplate;
23 
24     //@Test
25     public void test1() {
26         ClassInfo classInfo = new ClassInfo();
27         classInfo.setName("三年一班");
28         mongoTemplate.save(classInfo);
29 
30         Student student = new Student();
31         student.setName("张三");
32         student.setClassInfo(classInfo);
33         mongoTemplate.save(student);
34     }
35 
36     //@Test
37     public void test2() {
38         ClassInfo classInfo = mongoTemplate.findOne(Query.query(Criteria.where("id").is("5aaa30c0d678410bac20db1a")), ClassInfo.class);
39         Student student = new Student();
40         student.setName("李四");
41         student.setClassInfo(classInfo);
42         mongoTemplate.save(student);
43     }
44 
45     //@Test
46     public void test3() {
47         Student student = mongoTemplate.findOne(Query.query(Criteria.where("id").is("5aaa30c0d678410bac20db1b")), Student.class);
48         System.out.println(student);
49     }
50 
51 }
原文地址:https://www.cnblogs.com/tangzhe/p/8599639.html