java - 如何聚合嵌套属性中的查找?
问题描述
我有一个连接到 mongo db 的 Java Spring 应用程序。
在那个数据库中,我有两个集合:
atas 包含具有以下架构的文档:
{
ata: string,
description: string
}
和
包含具有以下架构的文档的更正:
{
//some properties
predictions: [
{ata: string, accuracy: float}
],
//other properties
}
我想在 atas 集合的 ata 属性和更正属性的 predictions 属性的 ata 属性上“加入”这两个集合,以便我最终得到一个文档
{
//some properties
predictions: [
{ata: string, description: string, accuracy: float}
]
}
或者
{
//some properties
predictions: [
{ata: {ata: string, description}, accuracy: float}
]
}
如何在 mongo 中做到这一点?
我使用以下代码来查看是否至少可以为每个预测获得正确的 ata 文档。
db.getCollection('corrections').aggregate([
{
$lookup:
{
from: "atas",
localField: "predictions.ata",
foreignField: "ata",
as: "atas"
}
}])
它在结果文档中为我提供了正确的 ata(如果它存在于 atas 集合中)。
现在我希望能够在每个文档的预测属性中的对象的 ata 属性中包含这些 atas,如上所述。
如果我的要求不明确,请不要犹豫,不要问我问题。
提前感谢您给我的任何帮助或时间。
PS:我用 java 和 spring 标记是因为我想在 Spring 中实现它,但首先我想看看如何在纯 mongo 中实现它。
解决方案
推荐阅读
- sqitch - 如何从失败的“sqitch check”中获取更多信息
- python - ModuleNotFoundError 没有名为“class1”的模块
- windows - 小米笔记本14显卡可以切换吗
- javascript - 做出反应。函数不会从 API 首次运行中保存
- django - Twilio REST Exception HTTP 429 错误:无法创建记录:请求过多
- python - 在 Teletbon 中更改用户权限
- reverse-proxy - OpenIO swift拒绝主机头
- javascript - 错误:重新渲染过多。React 限制了渲染的数量以防止无限循环。反应
- vue.js - 选中 Vue 2.x 未点击复选框
- bpf - 将字符串发送回 BPF 代码并返回用户空间时是否存在字符串大小限制?