在Spring Boot中集成Mongodb很是簡單,只須要加入Mongodb的Starter包便可,代碼以下:java
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
複製代碼
而後配置Mongodb的鏈接信息:mysql
spring.data.mongodb.uri=mongodb://192.168.0.13/test 複製代碼
完整配置信息請參考下面:spring
spring.data.mongodb.authentication-database= # Authentication database name. spring.data.mongodb.database= # Database name. spring.data.mongodb.field-naming-strategy= # Fully qualified name of the FieldNamingStrategy to use. spring.data.mongodb.grid-fs-database= # GridFS database name. spring.data.mongodb.host= # Mongo server host. Cannot be set with URI. spring.data.mongodb.password= # Login password of the mongo server. Cannot be set with URI. spring.data.mongodb.port= # Mongo server port. Cannot be set with URI. spring.data.mongodb.repositories.type=auto # Type of Mongo repositories to enable. spring.data.mongodb.uri=mongodb://localhost/test # Mongo database URI. Cannot be set with host, port and credentials. spring.data.mongodb.username= # Login user of the mongo server. Cannot be set with URI. 複製代碼
配置好了以後就直接能夠注入MongoTemplate操做數據了sql
首先建立一個實體類,咱們這邊用文章來作實體類,定義以下字段:mongodb
import java.util.Date; import java.util.List; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; /** * 文章信息 * @author yinjihuan * */ @Document(collection = "article_info") public class Article { @Id private String id; @Field("title") private String title; @Field("url") private String url; @Field("author") private String author; @Field("tags") private List<String> tags; @Field("visit_count") private Long visitCount; @Field("add_time") private Date addTime; //省略get set方法 } 複製代碼
實體類中的註解解釋以下: 1.Document註解標識這是一個文檔,等同mysql中的表,collection值表示mongodb中集合的名稱,不寫默認爲實體類名article。 2.Id註解爲主鍵標識 3.Field註解爲字段標識,指定值爲字段名稱,這邊有個小技巧,之全部spring-data.mongodb中有這樣的註解,是爲了可以讓用戶自定義字段名稱,能夠和實體類不一致,還有個好處就是能夠用縮寫,好比username咱們能夠配置成unane或者un,這樣的好處是節省了存儲空間,mongodb的存儲方式是key value形式的,每一個key就會重複存儲,key其實就佔了很大一份存儲空間。數據庫
接下來能夠操做數據庫了,接着上次,在測試類裏寫代碼數組
@Autowired private MongoTemplate mongoTemplate; /** * 初始化文章信息 * @author yinjihuan */ public static void initArticle() { //循環添加 for (int i = 0; i < 10; i++) { Article article = new Article(); article.setTitle("MongoTemplate的基本使用"); article.setAuthor("yinjihuan"); article.setUrl("http://cxytiandi.com/blog/detail/" + i); article.setTags(Arrays.asList("java", "mongodb", "spring")); article.setVisitCount(0L); article.setAddTime(new Date()); mongoTemplate.save(article); } //批量添加 List<Article> articles = new ArrayList<>(10); for (int i = 0; i < 10; i++) { Article article = new Article(); article.setTitle("MongoTemplate的基本使用"); article.setAuthor("yinjihuan"); article.setUrl("http://cxytiandi.com/blog/detail/" + i); article.setTags(Arrays.asList("java", "mongodb", "spring")); article.setVisitCount(0L); article.setAddTime(new Date()); articles.add(article); } mongoTemplate.insert(articles, Article.class); } 複製代碼
在數據量大的狀況下批量添加性能會更好哦。bash
//刪除author爲yinjihuan的數據 Query query = Query.query(Criteria.where("author").is("yinjihuan")); mongoTemplate.remove(query, Article.class); 複製代碼
//若是實體類中沒配集合名詞,可在刪除的時候單獨指定article_info query = Query.query(Criteria.where("author").is("yinjihuan")); mongoTemplate.remove(query, "article_info"); 複製代碼
//刪除集合,可傳實體類,也能夠傳名稱 mongoTemplate.dropCollection(Article.class); mongoTemplate.dropCollection("article_info"); 複製代碼
//刪除數據庫
mongoTemplate.getDb().dropDatabase();
複製代碼
下面這2種適合要知道刪除的數據有哪些的場景,好比記錄刪除的記錄微信
//查詢出符合條件的第一個結果,並將符合條件的數據刪除,只會刪除第一條 query = Query.query(Criteria.where("author").is("yinjihuan")); Article article = mongoTemplate.findAndRemove(query, Article.class); 複製代碼
//查詢出符合條件的全部結果,並將符合條件的全部數據刪除 query = Query.query(Criteria.where("author").is("yinjihuan")); List<Article> articles = mongoTemplate.findAllAndRemove(query, Article.class); 複製代碼
首先初始化須要修改的數據markdown
{ "_id": ObjectId("57c43caed4c63d7e39b5dc48"), "title": "MongoTemplate的基本使用", "url": "http://cxytiandi.com/blog/detail/0", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 0, "add_time": ISODate("2016-08-29T13:46:21.881Z") } { "_id": ObjectId("57c43caed4c63d7e39b5dc49"), "title": "MongoTemplate的基本使用", "url": "http://cxytiandi.com/blog/detail/1", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 0, "add_time": ISODate("2016-08-29T13:46:22.201Z") } 複製代碼
//修改第一條author爲yinjihuan的數據中的title和visitCount Query query = Query.query(Criteria.where("author").is("yinjihuan")); Update update = Update.update("title", "MongoTemplate").set("visitCount", 10); mongoTemplate.updateFirst(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現第一條數據的title還有visit_count被修改了
{ "_id": ObjectId("57c43caed4c63d7e39b5dc48"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/0", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:21.881Z") } { "_id": ObjectId("57c43caed4c63d7e39b5dc49"), "title": "MongoTemplate的基本使用", "url": "http://cxytiandi.com/blog/detail/1", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 0, "add_time": ISODate("2016-08-29T13:46:22.201Z") } 複製代碼
//修改所有符合條件的 query = Query.query(Criteria.where("author").is("yinjihuan")); update = Update.update("title", "MongoTemplate").set("visitCount", 10); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現全部數據的title還有visit_count被修改了
{ "_id": ObjectId("57c43caed4c63d7e39b5dc48"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/0", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:21.881Z") } { "_id": ObjectId("57c43caed4c63d7e39b5dc49"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/1", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:22.201Z") } 複製代碼
//特殊更新,更新author爲jason的數據,若是沒有author爲jason的數據則以此條件建立一條新的數據 //當沒有符合條件的文檔,就以這個條件和更新文檔爲基礎建立一個新的文檔,若是找到匹配的文檔就正常的更新。 query = Query.query(Criteria.where("author").is("jason")); update = Update.update("title", "MongoTemplate").set("visitCount", 10); mongoTemplate.upsert(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現新增了一條數據
{ "_id": ObjectId("57c43caed4c63d7e39b5dc48"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/0", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:21.881Z") } { "_id": ObjectId("57c43caed4c63d7e39b5dc49"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/1", "author": "yinjihuan", "tags": [ "java", "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:22.201Z") } { "_id": ObjectId("57c6e1601e4735b2c306cdb7"), "author": "jason", "title": "MongoTemplate", "visit_count": 10 } 複製代碼
//更新條件不變,更新字段改爲了一個咱們集合中不存在的,用set方法若是更新的key不存在則建立一個新的key query = Query.query(Criteria.where("author").is("jason")); update = Update.update("title", "MongoTemplate").set("money", 100); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現新加了一個key
{ "_id": ObjectId("57c6e1601e4735b2c306cdb7"), "author": "jason", "title": "MongoTemplate", "visit_count": 10, "money":100 } 複製代碼
//update的inc方法用於作累加操做,將money在以前的基礎上加上100 query = Query.query(Criteria.where("author").is("jason")); update = Update.update("title", "MongoTemplate").inc("money", 100); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現money變成200
{ "_id": ObjectId("57c6e1601e4735b2c306cdb7"), "author": "jason", "title": "MongoTemplate", "visit_count": 10, "money":200 } 複製代碼
//update的rename方法用於修改key的名稱 query = Query.query(Criteria.where("author").is("jason")); update = Update.update("title", "MongoTemplate").rename("visitCount", "vc"); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
{ "_id": ObjectId("57c6e1601e4735b2c306cdb7"), "author": "jason", "title": "MongoTemplate", "vc": 10, "money":200 } 複製代碼
//update的unset方法用於刪除key query = Query.query(Criteria.where("author").is("jason")); update = Update.update("title", "MongoTemplate").unset("vc"); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現vc這個key被刪除了
{ "_id": ObjectId("57c6e1601e4735b2c306cdb7"), "author": "jason", "title": "MongoTemplate", "money":200 } 複製代碼
//update的pull方法用於刪除tags數組中的java query = Query.query(Criteria.where("author").is("yinjihuan")); update = Update.update("title", "MongoTemplate").pull("tags", "java"); mongoTemplate.updateMulti(query, update, Article.class); 複製代碼
修改後結果以下,咱們會發現tags裏的java被刪除了
{ "_id": ObjectId("57c43caed4c63d7e39b5dc48"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/0", "author": "yinjihuan", "tags": [ "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:21.881Z") } { "_id": ObjectId("57c43caed4c63d7e39b5dc49"), "title": "MongoTemplate", "url": "http://cxytiandi.com/blog/detail/1", "author": "yinjihuan", "tags": [ "mongodb", "spring" ], "visit_count": 10, "add_time": ISODate("2016-08-29T13:46:22.201Z") } 複製代碼
查詢,不管是關係型數據庫仍是mongodb這種nosql,都是使用比較多的,大部分操做都是讀的操做。 mongodb的查詢方式不少種,下面只列了一些經常使用的,好比: 1.=查詢 2.模糊查詢 3.大於小於範圍查詢 4.in查詢 5.or查詢 6.查詢一條,查詢所有 7.本身慢慢學習...
根據做者查詢全部符合條件的數據,返回List
Query query = Query.query(Criteria.where("author").is("yinjihuan")); List<Article> articles = mongoTemplate.find(query, Article.class); 複製代碼
只查詢符合條件的第一條數據,返回Article對象
query = Query.query(Criteria.where("author").is("yinjihuan")); Article article = mongoTemplate.findOne(query, Article.class); 複製代碼
查詢集合中全部數據,不加條件
articles = mongoTemplate.findAll(Article.class);
複製代碼
查詢符合條件的數量
query = Query.query(Criteria.where("author").is("yinjihuan")); long count = mongoTemplate.count(query, Article.class); 複製代碼
根據主鍵ID查詢
article = mongoTemplate.findById(new ObjectId("57c6e1601e4735b2c306cdb7"), Article.class); 複製代碼
in查詢
List<String> authors = Arrays.asList("yinjihuan", "jason"); query = Query.query(Criteria.where("author").in(authors)); articles = mongoTemplate.find(query, Article.class); 複製代碼
ne(!=)查詢
query = Query.query(Criteria.where("author").ne("yinjihuan")); articles = mongoTemplate.find(query, Article.class); 複製代碼
lt(<)查詢訪問量小於10的文章
query = Query.query(Criteria.where("visitCount").lt(10)); articles = mongoTemplate.find(query, Article.class); 複製代碼
範圍查詢,大於5小於10
query = Query.query(Criteria.where("visitCount").gt(5).lt(10)); articles = mongoTemplate.find(query, Article.class); 複製代碼
模糊查詢,author中包含a的數據
query = Query.query(Criteria.where("author").regex("a")); articles = mongoTemplate.find(query, Article.class); 複製代碼
數組查詢,查詢tags裏數量爲3的數據
query = Query.query(Criteria.where("tags").size(3)); articles = mongoTemplate.find(query, Article.class); 複製代碼
or查詢,查詢author=jason的或者visitCount=0的數據
query = Query.query(Criteria.where("").orOperator( Criteria.where("author").is("jason"), Criteria.where("visitCount").is(0))); articles = mongoTemplate.find(query, Article.class); 複製代碼
更多技術分享請關注微信公衆號:猿天地