【发布时间】:2021-12-15 23:39:03
【问题描述】:
我正在开发 spring-boot-data-mongoDB。我在查询包含特定对象列表的嵌套文档时遇到了一些问题。
模拟类
@Document
public class Mock {
@Id
private String id;
@Indexed(unique = true)
private String name;
private List<Request> requests;
}
请求类
@Document
public class Request {
@Id
private String id;
private int status;
private String method;
private String endPoint;
private Map<String, Object> response;
private Map<String, Object> body;
private Map<String, String> params;
}
示例 JSON
[
{
_id: '53fc6dde-7a534-4b37-a57e-t0bd62f50046',
name: 'mock1',
requests: [
{
status: 200,
method: 'GET',
endPoint: 'status',
response: {},
body: {},
params: {}
}
],
_class: 'com.example.mockserverspring.models.Mock'
},
{
_id: '73fc6dde-7a5b-4b37-a57e-d0bd62f50046',
name: 'tester',
requests: [
{
_id: '802220ea-a1c7-484d-af1b-86e29b540179',
status: 200,
method: 'GET',
endPoint: 'api',
response: {
data: 'GET'
},
body: {
body: 'body'
},
params: {
params: 'params'
}
},
{
_id: 'ff8673d7-01a9-4d6f-a42e-0214a56b227b',
status: 200,
method: 'GET',
endPoint: 'data',
response: {},
body: {
data: 'data'
},
params: {
value: '10'
}
},
{
_id: '7fd5a860-b415-43b0-8115-1c8e1b95c3ec',
status: 200,
method: 'GET',
endPoint: 'status',
response: {},
body: {},
params: {}
}
],
_class: 'com.example.mockserverspring.models.Mock'
}
]
期望的查询输出:传入endPoint、mockName、body、params和method
- 从 db 中获取 mockName 的 mock 对象。
- 在返回的 mock 的 Requests List 中匹配 endPoint、body、params、method。
- 从符合上述所有条件的请求中返回响应字段。
从上面的示例json:
- 传入值:mockName:tester,方法:GET,endPoint:api,body:{body:'body'},params:{params:'params'}
- 这应该返回: 响应:{数据:'GET'}
- 当且仅当所有这些条件都匹配时,它才应该返回。
如有任何疑问,请告诉我。
【问题讨论】:
-
这是您需要的吗? mongoplayground.net/p/iHMzsUsQ8zn 如果是这样,我会发布完整的答案。
-
谢谢@RubénVega,但这会返回一个响应列表。 [ { "a": "返回这个响应" }, { "b": " and this one" }, { "c": " and this one" } ] .我将编辑问题只是为了清除它。
-
我想我误解了一些事情......这正是您需要的吗? mongoplayground.net/p/IFel2geB3EK
-
@RubénVega 谢谢,这正是我想要的。如何将此 mongoDB 查询与 springboot 应用程序一起使用?我尝试使用“@Query”、mongoTemplate 等。还将其添加为答案,以便将其标记为正确。感谢您的努力。
-
我没有使用 springboot 的经验,您应该查看其他类似的帖子。 stackoverflow.com/questions/59703147/…
标签: mongodb spring-boot mongodb-query spring-data spring-data-mongodb