Spring Boot中快速操作Mongodb数据库指南

前言

在Spring Boot中集成Mongodb非常简单,只需要加入Mongodb的Starter包即可,代码如下:


	org.springframework.boot
	spring-boot-starter-data-mongodb

然后配置Mongodb的连接信息:

spring.data.mongodb.uri=mongodb://192.168.0.13/test

完整配置信息请参考下面:

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操作数据了

添加数据

首先创建一个实体类,我们这边用文章来做实体类,定义如下字段:

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 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
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); }

在数据量大的情况下批量添加性能会更好哦。

删除操作

//删除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
articles = mongoTemplate.findAllAndRemove(query, Article.class);

修改操作

首先初始化需要修改的数据

{
    "_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
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 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);

总结

到此这篇关于Spring Boot中如何快速操作Mongodb的文章就介绍到这了,更多相关SpringBoot操作Mongodb内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(Spring Boot中快速操作Mongodb数据库指南)