【问题标题】:delete all records in mongoDB Spring application using CURL使用 CURL 删除 mongoDB Spring 应用程序中的所有记录
【发布时间】:2015-04-03 16:52:45
【问题描述】:

我正在使用 Spring 的一篇关于将 Spring 与 Mongo DB 集成的教程。 https://spring.io/guides/gs/accessing-mongodb-data-rest/

我只是希望能够使用 CURL 删除多条记录。

类似

curl -X DELETE http://localhost:8080/people/

本教程展示了如何删除特定记录而不是多条记录。与 CURL 一起工作也是相当新的......我很确定我错过了一些简单的东西,谢谢。 仅供参考,删除单个记录的方法是

curl -X DELETE http://localhost:8080/people/53149b8e3004990b1af9f229

【问题讨论】:

    标签: java spring mongodb spring-mvc


    【解决方案1】:

    我想,你应该运行这个

    mongo <dbname> --eval "db.people.drop()"
    

    【讨论】:

      【解决方案2】:

      当您运行链接的示例项目时,Spring Boot 会自动连接一个 RepositoryEntityController,它基本上公开了两个 URI:

      http://localhost:8080/people/
      

      http://localhost:8080/people/{id}
      

      {id} 是特定人员的 ID。

      它还提供了一些方法并将它们与上述 URI 之一绑定。例如,有 getCollectionResource 方法,它“监听”http://localhost:8080/people/,所以你可以运行

      curl http://localhost:8080/people/
      

      并获取保存在数据库中的人员列表。

      另一方面,deleteItemResource 方法“监听”http://localhost:8080/people/{id},因此特定的controller 不提供同时删除所有人员实体的功能。

      但您始终可以编写自己的控制器并提供自定义功能。下面的简单代码将完成工作(但当然不包括所有其他方法):

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.RestController;
      
      @RestController
      @RequestMapping("/people")
      public class PersonController {
      
          @Autowired
          PersonRepository people;
      
          @RequestMapping(method=RequestMethod.DELETE)
          public void deleteAll() {
              people.deleteAll();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-29
        • 2017-03-28
        • 2018-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-08
        • 2015-09-21
        相关资源
        最近更新 更多