MongoDB - 具有匹配的聚合和限制为分组字段的最大数量

时间:2017-09-17 05:00:23

标签: mongodb aggregation-framework

我认为我错过了MongoDB Aggregate Pipeline知识的一个关键部分,以便接近我想要做的事情。

这是我的数据:

[
   {frameId: <some unique guid>, videoSessionId: 1, subject: "John", "createdBy": "personA", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 1, subject: "John", "createdBy": "personA", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 1, subject: "John", "createdBy": "personA", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 2, subject: "John", "createdBy": "personA", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": true},
   {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": true},
   {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 4, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 4, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 5, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 5, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 6, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 6, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 7, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 7, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 7, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 7, subject: "Mary", "createdBy": "personB", "blurry": false},
   {frameId: <some unique guid>, videoSessionId: 7, subject: "Mary", "createdBy": "personB", "blurry": false},
]

我想查询数据并收到:

[
       {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": false},
       {frameId: <some unique guid>, videoSessionId: 3, subject: "John", "createdBy": "personB", "blurry": false},
       {frameId: <some unique guid>, videoSessionId: 4, subject: "Mary", "createdBy": "personB", "blurry": false},
       {frameId: <some unique guid>, videoSessionId: 4, subject: "Mary", "createdBy": "personB", "blurry": false},
       {frameId: <some unique guid>, videoSessionId: 5, subject: "Mary", "createdBy": "personB", "blurry": false},
       {frameId: <some unique guid>, videoSessionId: 5, subject: "Mary", "createdBy": "personB", "blurry": false},    
]

这是我想要的明文版本:

&#34;在personB创建的所有非模糊帧中,每个主题给我最多2个videoSessions。&#34;

更深一点:

  • 以上查询的数据只包含一个videoSession的数据,因为John只有1个由personB创建的videoSession。
  • 上述查询数据仅包含Mary的4个视频会话中的2个。 虽然她有4个由personB创建并且不模糊的视频会话,但我们希望每个主题最多只能收集2个视频会话。

我目前正在解决这个问题,使用以下方法。这对我来说似乎效率低下,因为它需要查找,展开,替换root和另一个匹配。

  1. $ match createdBy = personB and blurry = false
  2. $ subject by subject和$ addToSet of videoSessionId获取videoSessionIds数组
  3. 一个$项目,它使用maxNumber的$ slice来限制每个主题的videoSessionIds
  4. 在同一个集合上通过videoSessionId进行$ lookup和$展开以填充原始数据
  5. 一个$ replaceRoot,因为$ unwind将它放入主题
  6. 另一个$ match for createdBy = personB和blurry = false - &gt;因为$ lookup和$ unwind给了我所有记录回来的特定videoSessionIds我发现我想要的。

0 个答案:

没有答案