转:https://www.jianshu.com/p/dd7b5a0e2f64
今天我们学习如何使用spring-data-mongodb来对集合中的数据进行修改操作
首先准备要练习的数据2条,实体类Article的定义请参考之前的文章spring-data-mongodb之MongoTemplate 添加数据
{
"_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")
}
源码地址:https://github.com/yinjihuan/cxytiandi