无法使用$ elemMatch查找匹配的文档

时间:2020-04-04 17:12:17

标签: node.js mongodb

我有一个JSON数据,例如:

[
    {
        "_id": "5e887ee61c7f053dc8702214",
        "leadName": "Experienced",
        "leads": [
            {
                "name": "Joey",
                "mobile": 1524524678,
                "_id": "5e887ee61c7f053dc8702212",
                "leadType": "5e887a470962a53b80e0b57a"
            },
            {
                "name": "Ben",
                "mobile": 1524524678,
                "_id": "5e887ee61c7f053dc8702213",
                "leadType": "5e887a470962a53b80e0b57a"
            }
        ]
        "__v": 0
    },
    {
        "_id": "5e887fa91c7f053dc8702217",
        "leadName": "Fresher",
        "leads": [
            {
                "name": "Rachel",
                "mobile": 1524524678,
                "_id": "5e887fa91c7f053dc8702215",
                "leadType": "5e887a470962a53b80e0b578"
            },
            {
                "name": "Chandler",
                "mobile": 1524524678,
                "_id": "5e887fa91c7f053dc8702216",
                "leadType": "5e887a470962a53b80e0b57a"
            }
        ]
        "__v": 0
    }
]

属于leads数组的每个对象都有一个与其关联的leadTypeleadType是另一个架构的参考_id。我在这里尝试访问的是与作为请求传递的特定leadType匹配的所有对象/文档。

我有四分之三的用户有一个与leadType相关联的"leadType": "5e887a470962a53b80e0b57a"

当我使用"leadType": "5e887a470962a53b80e0b57a"查询以检索这3个用户的详细信息时,它仅与对象的第一个数组中的单个用户匹配。和第二个对象数组中的一个。

我尝试了以下查询:

exports.leads_by_type = (req, res) => {
  const { leadtypeid } = req.body;

  Lead.find({"leads.leadType": leadtypeid}, {leads: { $elemMatch : {leadType: leadtypeid}}}, (err, result) => {
    res.json(result)
  });
  //this query returns only the first document of first object's array of objects and one from second.


  //OR EVEN THIS ONE       

  Lead.aggregate([
    {$match: {userId: req.user.userId}},
    {$unwind: "$leads"},
    {$match: {"leads.leadType": leadtypeid}}
  ], (err, test) => {
    res.json({test})
  });
  //this query returns empty array []

};

我需要返回一个或多个与给定ID匹配的每个文档。在SO和这个特定的问题Make $elemMatch (projection) return all objects that match criteria(看起来像我的用例)上,我尝试了很多方法,但是似乎都没有一个能解决我的问题。

请帮助解决此问题。这真的很重要。

1 个答案:

答案 0 :(得分:1)

您的第二个解决方案将返回一个空数组,因为您在第一个_id = leadtypeid中使用$match搜索文档,但是没有具有此ID的文档,我的意思是document是整个对象,有

_id = "5e887ee61c7f053dc8702214" or "5e887fa91c7f053dc8702217"

只要您只对leadtypeid感兴趣,那么我们可以将其添加到第一个$ match中,但这还不够,因为这将搜索具有该线索类型的文档,因此结果将包含你不想的第四领先 这就是为什么我们使用$ unwind来在单独的文档中获取具有潜在顾客类型的每个文档的原因 然后匹配所需的潜在客户类型,以摆脱第四种潜在客户类型

所以查询将是这样

注意:将'5e887a470962a53b80e0b57a'替换为leadtypeid

db.collectionName.aggregate(
    [
        {
            $match: {
                'leads.leadType': '5e887a470962a53b80e0b57a' // this to find all the documents which have this lead type
            }
        },
        {
            $unwind: '$leads' // to have multiple documents, each document with leads array of only one item in it, the lead type we are searching for only
        },
        {
            $match: {
                'leads.leadType': '5e887a470962a53b80e0b57a' // to get the documents that have this lead type and get rid of any other lead type
            }
        }
    ]
)

此查询的结果将为

{
    "_id" : "5e887ee61c7f053dc8702214",
    "leadName" : "Experienced",
    "leads" : {
        "name" : "Joey",
        "mobile" : 1524524678,
        "_id" : "5e887ee61c7f053dc8702212",
        "leadType" : "5e887a470962a53b80e0b57a"
    }
}
{
    "_id" : "5e887ee61c7f053dc8702214",
    "leadName" : "Experienced",
    "leads" : {
        "name" : "Ben",
        "mobile" : 1524524678,
        "_id" : "5e887ee61c7f053dc8702213",
        "leadType" : "5e887a470962a53b80e0b57a"
    }
}
{
    "_id" : "5e887fa91c7f053dc8702217",
    "leadName" : "Fresher",
    "leads" : {
        "name" : "Chandler",
        "mobile" : 1524524678,
        "_id" : "5e887fa91c7f053dc8702216",
        "leadType" : "5e887a470962a53b80e0b57a"
    }
}

希望这就是您要寻找的