mongodb - 从 MongoDB 中获取嵌套数据
问题描述
我有一个以下格式的集合。
{
"_id": "ffffc446-f33d",
"className": "com.ezdx.vist.model.Visit",
"organizationId": "0a0beff7-fe1e-4ab7",
"centerId": "9aef68fe-dffd-4a7d-b0ee-f8dd3fc03303",
"tests": [{
"result": 157,
"type": "PHYSICAL",
**"name": "HEIGHT",**
"consumableQuantity": 0,
"testCost": 0,
"testExpenseConsumable": 0
},
{
"result": 8,
"type": "RDT",
**"name": "URIC",**
"consumableQuantity": 0,
"testCost": 0,
"testExpenseConsumable": 0
}
],
"repeatVisit": true
}
我想要 test.name = "Uric" 和特定列的集合。
{
"result": 8,
"type": "RDT",
**"name": "Uric",**
"consumableQuantity": 0,
"testCost": 0,
"testExpenseConsumable": 0
}
不知何故,我设法获得了所需的收藏,但我无法获得所需的格式。以下是我的查询
db.visits.aggregate( [ { $unwind : "$tests" },
{ $match: { $and: [{"tests.name":"URIC"}]
} } ] )
解决方案
试试这个:$replaceWith (=v4.2) 或$replaceRoot (>=v3.4)
db.visits.aggregate([
{
$unwind: "$tests"
},
{
$match: {
"tests.name": "URIC"
}
},
{
$replaceWith: "$tests"
}
])
推荐阅读
- video - 在 windows server 2008 R2 上访问 mp4 文件说访问被拒绝
- c - 如何在 C 的 1 行中拆分字符串(字符)和变量?
- mongodb - Process.StandardInput.Write 未使用 .Net Core 写入 MacOS 上的终端
- spring-boot - spring-boot + kotlin + Junit中的单元测试POST API
- login - 如何使用 Jmeter 登录 LDAP?
- java - 我简单的 Java pi 计算器似乎总是给出无穷大?
- java - 为什么在 ViewModelProvider.Factory 实现中添加 @Singleton 注解会导致编译错误 [Dagger/MissingBinding]?
- javascript - HTTP500:在服务器上使用 ajax POST 但不是 localhost 时出现服务器错误
- c# - 将 X509Certificate2.PrivateKey 解析为 RSACryptoServiceProvider 时出错
- ios - 我可以覆盖prepare for segue 方法以使用UIViewController 扩展添加一些代码吗?