在MongoDB中找到匹配的记录,其中一条记录来自数组

在MongoDB中找到匹配的记录,其中一条记录来自数组,mongodb,Mongodb,我有一个联系人集合,我需要找到一个或多个与数组中的记录匹配的联系人 该数组如下所示: [ { linkType: "email", "bob@example.com" }, { linkType: "email", "joan@example.com" } { linkType: "customerID", "12345" } ] { "code": "cust01", "contactLink": [ { "li

我有一个联系人集合,我需要找到一个或多个与数组中的记录匹配的联系人

该数组如下所示:

[
    { linkType: "email", "bob@example.com" },
    { linkType: "email", "joan@example.com" }
    { linkType: "customerID", "12345" }
]
{
    "code": "cust01",
    "contactLink": [
        {
            "linkType": "email",
            "value": "joe@example.com"
        },
        {
            "linkType": "customerID",
            "value": "12345"
        }
    ],
    "name": "joe bloggs"
}
联系人记录(减少)如下所示:

[
    { linkType: "email", "bob@example.com" },
    { linkType: "email", "joan@example.com" }
    { linkType: "customerID", "12345" }
]
{
    "code": "cust01",
    "contactLink": [
        {
            "linkType": "email",
            "value": "joe@example.com"
        },
        {
            "linkType": "customerID",
            "value": "12345"
        }
    ],
    "name": "joe bloggs"
}
我只需要数组中的一个匹配项,但两个字段都需要匹配,结果可能会返回多个记录。如何在不进行多个查找(迭代数组)的情况下执行此操作

谢谢您的时间。

我们可以使用它对数组元素应用过滤器。以下是一个例子:

db.collection.find({
    "contactLink":{
        $elemMatch:{
            "linkType": "email",
            "value": "joe@example.com"
        }
    }
}).pretty()
数据集:

{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7d"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "joe@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}
{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7e"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "josh@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}
{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7d"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "joe@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}
输出:

{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7d"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "joe@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}
{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7e"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "josh@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}
{
    "_id" : ObjectId("5d89b84c9c5291d5f4963e7d"),
    "code" : "cust01",
    "contactLink" : [
        {
            "linkType" : "email",
            "value" : "joe@example.com"
        },
        {
            "linkType" : "customerID",
            "value" : "12345"
        }
    ],
    "name" : "joe bloggs"
}