我有3个系列

Users

{
 "_id": "P4SpYVd1KjBaF4SKyVw0E",
  "login": "User-01",
  "name": "John",
  "lastName": "Doe",
}

Moods

{
 "source": {
     "userId": "P4SpYVd1KjBaF4SKyVw0E",
  },
  "timestamp": "2022-06-11T12:44:13.333Z",
  "mood": "bad",
  "_id": "62a352b83859aaf975c6332d",
}

contactRequest

{
  "timestamp": "2022-06-11T15:25:13.333Z",
  "source" : {
     "userId":"P4SpYVd1KjBaF4SKyVw0E"
   },
  "resolve": false,
  "_id": "62a351ff3859aaf975c63329"
}

I want to achieve the final document which will looks like this:
Final

{
 "_id": "P4SpYVd1KjBaF4SKyVw0E",
  "login": "User-01",
  "name": "John",
  "lastName": "Doe",
  "calendar": [
      {
        "timestamp": "2022-06-11T12:44:13.333Z"       //which is moods.timestamp,
        "mood": {
            "source": {
              "userId": "P4SpYVd1KjBaF4SKyVw0E",
             },
             "timestamp": "2022-06-11T12:44:13.333Z",
             "mood": "bad",
             "_id": "62a352b83859aaf975c6332d",
         },
         "contactRequest": [
             {
               "timestamp": "2022-06-11T15:25:13.333Z", //contactRequest is grouping by date with moods
               "source" : {
                 "userId":"P4SpYVd1KjBaF4SKyVw0E"
               },
               "resolve": true,
               "_id": "62a351ff3859aaf975c63329"
             },
             {
               "timestamp": "2022-06-11T18:23:13.333Z", 
               "source" : {
                 "userId":"P4SpYVd1KjBaF4SKyVw0E"
               },
               "resolve": false,
               "_id": "62a351ff3859aaf975c63329"
             },
         ]
        }
      }
   ]
}

我try 用$lookup个参数对其进行聚合,但它在用户文档中给了我两个单独的字段,这并不令人满意
我也使用了$project参数,但问题是按日期分组结果

这里有一个mongoplayground.net次try ,但并不能正确返回所有内容.

推荐答案

使用问题的mongoplayground作为起点,我对其进行了一些修改,以便按要求返回文档.

db.users.aggregate([
  {
    "$match": {
      // id comes from param
      "_id": "P4SpYVd1KjBaF4SKyVw0E"
    }
  },
  {
    "$lookup": {
      "from": "moods",
      "localField": "_id",
      "foreignField": "source.userId",
      "pipeline": [
        {
          "$lookup": {
            "from": "contactRequests",
            "localField": "source.userId",
            "foreignField": "source.userId",
            "let": {
              // truncate timestamp to start of day
              "moodsDate": { "$dateTrunc": { "date": "$timestamp", "unit": "day" } }
            },
            "pipeline": [
              {
                "$match": {
                  "$expr": {
                    "$eq": [
                      "$$moodsDate",
                      { // truncate timestamp to start of day
                        "$dateTrunc": { "date": "$timestamp", "unit": "day" }
                      }
                    ]
                  }
                }
              }
            ],
            "as": "contactRequests"
          }
        },
        {
          // don't return mood doc if no requests
          "$match": {
            "$expr": { "$gt": [ { "$size": "$contactRequests" }, 0 ] }
          }
        }
      ],
      as: "calendar"
    }
  }
])

试试mongoplayground.net码.

Javascript相关问答推荐

如何判断属于多个元素的属性是否具有多个值之一

在页面上滚动 timeshift 动垂直滚动条

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

函数返回与输入对象具有相同键的对象

您能在卸载程序(QtInsteller框架)上添加WizardPage吗?

DOM不自动更新,尽管运行倒计时TS,JS

MarkLogic-earch.suggest不返回任何值

Google脚本数组映射函数横向输出

为什么在函数中添加粒子的速率大于删除粒子的速率?

每次重新呈现时调用useState initialValue函数

用于测试其方法和构造函数的导出/导入类

是否可以在不更改组件标识的情况下换出Reaction组件定义(以维护状态/引用等)?如果是这样的话,是如何做到的呢?

如何使用[ModelJSON,ArrayBuffer]调用tf.loadGraphModelSync

按特定顺序将4个数组组合在一起,按ID分组

如何压缩图像并将其编码为文本?

有没有办法在R中创建一张具有多个色标的热图?

Played link-Initialize.js永远显示加载符号

如何用react组件替换dom元素?

不允许在对象文本中注释掉的属性

我如何才能阻止我的球数以指数方式添加到我的HTML画布中?