- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有 2 个集合(带有示例文档):
报告
{
id: "R1",
type: "xyz",
}
报告文件
{
id: "F1",
reportid: "R1",
time: ISODate("2016-06-13T14:20:25.812Z")
},
{
id: "F14",
reportid: "R1",
time: ISODate("2016-06-15T09:20:29.809Z")
}
如您所见,一个报告
可能有多个报告文件
。
我想执行一个查询,匹配一个报告 id
,按原样返返回告文档,加上一个附加键存储作为子文档的 reportfile
最近的 time
(如果没有 reportid
会更好,因为它会是多余的),例如
{
id: "R1",
type: "xyz",
reportfile: {
id: "F14",
reportid: "R1",
time: ISODate("2016-06-15T09:20:29.809Z")
}
}
我的问题是每个报告类型都有自己的一组属性,因此在聚合管道中使用 $project
不是最好的方法。
到此为止
db.reports.aggregate([{
$match : 'R1'
}, {
$lookup : {
from : 'reportfiles',
localField : 'id',
foreignField : 'reportid',
as : 'reportfile'
}
}
])
当然会以“reportfile”的形式返回具有给定 reportid
的所有文件的列表。我如何有效地过滤该列表以获得我需要的唯一元素?
高效 -> 我尝试使用 $unwind
作为下一个管道步骤,但生成的文档长得吓人而且毫无意义。
提前感谢您的任何建议!
最佳答案
您需要添加另一个 $project
在 $lookup
之后阶段到您的聚合管道阶段。
{ "$project": {
"id": "R1",
"type": "xyz",
"reportfile": {
"$let": {
"vars": {
"obj": {
"$arrayElemAt": [
{ "$filter": {
"input": "$reportfile",
"as": "report",
"cond": { "$eq": [ "$$report.time", { "$max": "$reportfile.time" } ] }
}},
0
]
}
},
"in": { "id": "$$obj.id", "time": "$$obj.time" }
}
}
}}
$filter
运算符“过滤”$lookup
结果并返回包含满足条件的文档的数组。这里的条件是$eq
当文档有 $max
时返回真最小值。
$arrayElemAt
运算符 slice $filter 的结果并返回数组中的元素,然后使用 $let
将其分配给变量运算符(operator)。从那里,您可以使用 dot notation 轻松访问结果中所需的字段。 .
关于mongodb - 过滤 $lookup 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37833808/
如果查找返回 null 该怎么办?我正在使用 org.openide.util.Lookup 的 Lookup.getDefault().lookup() ,它用于查找对象的实例。一般模式是传递一个
我尝试了两种方法来获取给定函数的 MethodHandle。 Method 1 Method m = MyClass.class.getMethod("myMethod", String.class,
前言 前面几节都是讲的基础内容,本节我们讲讲索引性能优化,当对大数据进行处理时首先想到的就是索引,一旦遇到这样的问题则手忙脚乱,各种查资料,为何平常不扎实基本功呢,我们由浅入深,简短的内容,深入的
我试图理解 System.Linq.Lookup类(class)。调用GetType()时,显示如下: System.Linq.Lookup`2+Grouping[System.Object,Syst
什么是依赖于参数的查找有哪些好的解释?许多人也称其为Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么不好? 如何运作? 最佳答案 Koenig查找或 Argument Dep
关于什么是参数相关查找有什么好的解释?许多人也称它为 Koenig Lookup。 最好我想知道: 为什么是好事? 为什么是坏事? 它是如何工作的? 最佳答案 Koenig 查找 , 或 Argume
我有两个 mongo 集合,一个包含约会提醒,另一个包含通知。我正在尝试返回给定的branchId/clinic_id中的所有存档:错误提醒的结果,并包括其已确认的:错误通知。另外,我想确保约会显示在
关于什么是参数依赖查找有什么好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查询 , 或
我正在 AX 2009 中的一个表单上创建一个项目。有一个名为“SubsPersonName”的查找字段,它使用以下方法对 ContactPerson 表执行查找。 public void looku
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何运作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有哪些好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
关于什么是参数依赖查找有什么好的解释?许多人也将其称为 Koenig Lookup。 最好我想知道: 为什么这是一件好事? 为什么这是一件坏事? 它是如何工作的? 最佳答案 Koenig 查找,或 A
以下函数将电话号码作为输入参数(例如 +436641234567 或 +436641234567)并在联系人数据库中执行两次查找:首先,识别属于该号码的用户(这已经有效),然后使用该 ID用户的 获取
我设置了一个运行centos6.2 64位的virtualbox客户端机器,主机是windows7 64位,并在客户端机器上设置了两个网卡,一个是NAT模式,一个是host-only模式,然后我就陷入
我是一名优秀的程序员,十分优秀!