【问题标题】:How to delete object from list in JSON using SpringBoot and MongoDB如何使用 Spring Boot 和 MongoDB 从 JSON 列表中删除对象
【发布时间】:2022-01-21 23:53:23
【问题描述】:

在我的项目中,我使用的是 Spring Boot 和 MongoDB。

我想从 objectId 的 JSON 列表中删除对象:

我在数据库中有一个配置文件对象,如下所示:

{
    "_id" : ObjectId("61c05611a8237a39811ec3c0"),
    "runningStyle" : [ 
        {
            "_id" : ObjectId("61c05616a8237a39811ec3c1"),
            "name" : "name1",
            "priority" : "priority1",
            "average" : "1"
        }, 
        {
            "_id" : ObjectId("61c05616a8237a39811ec3c2"),
            "name" : "name2",
            "priority" : "priority2",
            "average" : "2"
        }, 
        {
            "_id" : ObjectId("61c05616a8237a39811ec3c3"),
            "name" : "name3",
            "priority" : "priority3",
            "average" : "3"
        }
    ]
}

我想通过 Profile 对象中列表 runningStyle 中的 ID 删除 object

这是我在代码中尝试过的:

RunningController.java

@DeleteMapping(value = "/delete/{runningStyleId}")
public void deleteRunningStyle(@PathVariable String runningStyleId){
   playingStyleService.deletePlayingStyle(playingStyleId);
}

RunningServiceImpl.java

@Override
public void deletePlayingStyle(String playingStyleId) {
        profile.ifPresent(up -> savedPlayingStyle.ifPresent(sps -> {
           
            up.getPlayingStyle().stream().filter(am -> am.getId().equals(playingStyleId))
                    .findFirst()
                    .ifPresent(ps -> profileRepository.deleteById(ps.getId()));
          
            playingStyleRepository.deleteById(playingStyleId);
        }));
}

使用此代码,我没有任何错误,但对象并未从我的个人资料列表中删除。

我也尝试在存储库中添加类似的内容:

@Query(value = "{ '_id' : { '$oid' : ?0}}", delete = true)
void deleteRunningStyleById(String runningStyleId);

但它并没有删除它。

我做错了什么?实现这一目标的最佳方法是什么?

更新

我已添加:

@Query(value = "{'$pull': {'runningStyle':{'_id' : ?0 }}}", delete = true)
void deleteRunningStyleById(String runningStyleId);

但我收到以下错误:

unknown top level operator: $pull. If you have a field name that starts with a '$' symbol, consider using $getField or $setField.

【问题讨论】:

  • 你试过$pull吗?
  • 我没有你能举个例子吗?
  • 尝试运行这个:db.collection.updateMany({}, {$pull:{runningStyle:{"_id" : ObjectId("61c05616a8237a39811ec3c2")}}})。如果可行,将其转换为 Spring-Data 语法
  • 我尝试添加它,但收到错误消息。请看我的问题更新
  • 检查我的答案

标签: java mongodb spring-boot spring-data-mongodb


【解决方案1】:

致电$pull

@Autowired
private MongoTemplate mongoTemplate;
...

@Override
public void deletePlayingStyle(String playingStyleId) {
    Update update = new Update();
    // $pull
    update.pull("runningStyle", new Document("_id", new ObjectId(playingStyleId)));
    
    //UpdateResult { "acknowledged" : true, "matchedCount" : x, "modifiedCount" : y }
    //check if modifiedCount > 0 to make sure profile updated
    mongoTemplate.updateMulti(new Query(), update, Profile.class);
}

【讨论】:

  • 感谢您的回答,但是否可以将其调整为我已有的代码?
  • 不,@Query 用于.find 而不是.updateXXX
猜你喜欢
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2023-02-15
  • 2020-09-12
  • 1970-01-01
  • 2021-03-30
相关资源
最近更新 更多