java - MongoDB & Spring Boot:如何根据过滤器值从数组中仅获取特定的子文档?
问题描述
我对 MongoDB 有点陌生,并尝试使用 Spring Boot 和 Java 编写查询以仅从数组中获取匹配的子文档,但是无论我尝试如何,我都会在响应中不断获取整个文档。
我的示例数据集如下所示:
{
"storeId": "xxxxx",
"bu": "xxxxx",
"items": [
{
"itemId": "1234",
"itemName": "x",
"price": "10.0",
},
{
"itemId": "43556",
"itemName": "y",
"price": "1.0"
}]
}
{
"storeId": "yyyyyy",
"bu": "xxxxx",
"items": [
{
"itemId": "1234",
"itemName": "x",
"price": "0.0",
},
{
"itemId": "67654",
"itemName": "y",
"price": "10.0"
}]
}
我想过滤 items.itemId = 1234 和 items.price =10.0,我期待以下输出:
{
"storeId": "xxxxx",
"bu": "xxxxx",
"items": [
{
"itemId": "1234",
"itemName": "x",
"price": "10.0",
}]
}
但是我得到
{
"storeId": "xxxxx",
"bu": "xxxxx",
"items": [
{
"itemId": "1234",
"itemName": "x",
"price": "10.0",
}]
}
{
"storeId": "yyyyyy",
"bu": "xxxxx",
"items": [
{
"itemId": "1234",
"itemName": "x",
"price": "0.0",
}]
}
有人可以帮我用 Spring boot 和 mongotempalte 框架编写这个查询吗?
解决方案
推荐阅读
- cryptography - 是否可以将 asp.net hash+salt 与 HIBP 数据库进行比较?
- qt - ListView如何实现更好的缓存控制?
- python - 如何按接收顺序获取 Counter 对象的值?
- php - 如何让“php artisan cache: clear”命令工作?
- tokenize - 葡萄牙语分词器:t 打破“a”和“o”中的“ao”
- c# - 按下 Outlook 电子邮件发送按钮后调用 C# 方法或函数?
- php - getAccessToken() 方法在 facebook PHP SDK 5 中返回 null
- excel - 用于计算目标进度百分比的动态公式?
- sql - 在 Oracle sql 中以 T 为状态的最后更改数据
- php - get_meta_tags 用于批处理的慢速替代方案