在Mongoose中展开内部查找

时间:2019-01-29 16:50:14

标签: node.js mongodb mongoose

我有2个要合并的实体:客户反馈。反馈中包含一系列嵌入式投票( Upvote

对于特定反馈,客户不能多次投票。 我要实现的是-给定一个特定的反馈ID-获得具有附加虚拟属性的客户的完整列表,该属性说明他/她是否赞成给定的反馈。

Customer.aggregate(
    [
      {
        $match: { company_id: new ObjectID(req.user.company_id) }
      },
      {
        $lookup: {
          from: 'feedbacks',
          let: { 'c_id': '$_id' },
          pipeline: [
            {
              $unwind: '$upvotes'
            },
            {
              $match: { $expr: { $eq: ['$upvotes.customer_id._id', '$$c_id'] } }
            }
          ],
          as: 'upvotes'
        }
      }
    ],
    function(err, customers) {
      if (err) {
        console.log(err);
        res.status(400).send(err);
      } else {
        res.send({ customers });
      }
    }
  );

为此,我必须查看针对特定反馈的支持列表,然后使用customer_id将其与客户表一起加入。

上述方法无效。有什么建议我做错了吗?

样本数据(反馈)

{
        "size": 0,
        "points": 50,
        "status": "open",
        "potential": 0,
        "real": 5000,
        "_id": "5c3d033271ceb7edc37d156c",
        "title": "Custom Invoice Templates",
        "description": "Provide an editor to create custom invoices.",
        "owner_id": {
            "_id": "5c3b684f7cec8be977c2a465",
            "email": "maurizio@acme.com"
        },
        "company_id": "5c3b684f7cec8be977c2a462",
        "project_id": "5c3b68507cec8be977c2a468",
        "upvotes": [
            {
                "_id": "5c3fa5b371ceb7edc37d159a",
                "comments": "bbbb",
                "priority": "should",
                "customer_id": {
                    "size": 0,
                    "potential": 0,
                    "real": 5000,
                    "_id": "5c3b68507cec8be977c2a485",
                    "name": "Oyomia Ltd."
                },
                "owner_id": {
                    "_id": "5c3b684f7cec8be977c2a465",
                    "email": "maurizio@acme.com"
                }
            }
        ],
        "updatedAt": "2019-01-16T21:44:19.215Z",
        "createdAt": "2019-01-14T21:46:26.286Z",
        "__v": 0
    }

样本数据(客户)

{
        "size": 0,
        "potential": 0,
        "real": 5000,
        "_id": "5c3b68507cec8be977c2a485",
        "name": "Oyomia Ltd.",
        "contact": {
            "_id": "5c40f8de71ceb7edc37d15ab",
            "name": "Nick Page",
            "email": "np@oyoma.com"
        },
        "company_id": "5c3b684f7cec8be977c2a462",
        "deals": [
            {
                "value": 5000,
                "_id": "5c3b68507cec8be977c2a487",
                "name": "Armour batch",
                "status": "won",
                "type": "non_recurring",
                "updatedAt": "2019-01-13T16:33:20.870Z"
            }
        ],
        "__v": 0,
        "updatedAt": "2019-01-17T21:51:26.877Z"
    }

0 个答案:

没有答案