簡體   English   中英

如何從另一個Array MongoDB中的一個數組計算匹配元素

[英]How to count matched elements from an Array in another Array MongoDB

我正在使用MongoDB Aggregate框架來查詢用戶集合。

這是下面的用戶架構布局的示例:

{
    "_id" : ObjectId("XXXXXXXXXXXXXX367db"),
        "updatedAt" : ISODate("2017-08-18T10:59:54.904Z"),
            "createdAt" : ISODate("2017-08-18T10:59:54.904Z"),
                "email" : "fake57@gmail.com",
                    "firstName" : "Tianna.",
                        "gender" : "male",
                            "geometry" : {
        "coordinates" : [
            -6.26119,
            53.35247
        ],
            "_id" : ObjectId("5996c8a9a4d84d3639c367dc"),
                "type" : "point"
    },
    "age" : 25,
        "personalAttributes" : [
            "ksjdnksajdna",
            "ksjdssacasca",
            "xz12nksajdna",
            "xz12nksaxxna",
            "xz12nksaxxxx",
            "xz12nwwzwwwa",
            "xz12nkslkmna",
        ]
}

這是聚合管道中概述的步驟。

1:使用$ geoNear運算符在指定距離內定位用戶。

2:根據性別匹配用戶。

db.getCollection('users').aggregate([
    {
        "$geoNear": {
            "near": {
                "type": "Point",
                "coordinates": [
                    -6.26030969999999,
                    53.3498053
                ]
            },
            "distanceField": "dist.calculated",
            "spherical": true,
            "maxDistance": 770000
        }
    },
    {
        "$match": {
            "gender": "male"
        }
    }
])

我想要做的是傳遞另一個用戶的personalAttributes數組並計算每個數組中匹配項的數量。看起來像這樣:

db.getCollection('users').aggregate([
    {
        "$geoNear": {
            "near": {
                "type": "Point",
                "coordinates": [
                    -6.26030969999999,
                    53.3498053
                ]
            },
            "distanceField": "dist.calculated",
            "spherical": true,
            "maxDistance": 770000
        }
    },
    {
        "$match": {
            "gender": "male"
        }
    },
    {
        "$project": {
            "_id": 1,
            "gender": 1,
            "firstName": 1,
            "profileImage": 1
                "personalAttributesMatches": {
                //Pass in the users personalAttributes Array and count the number of matches and return all the data. 
                }
        }
    }

 }
])

與預期的輸出是

/* 1 */
{
    "_id" : ObjectId("5996c8aaa4d84d3639c36a61"),
    "firstName" : "Sharon",
    "gender" : "male",
    "personalAttributesMatches": 15
}

/* 2 */
{
    "_id" : ObjectId("5996c9c41daf273658715fcf"),
    "firstName" : "Hilton",
    "gender" : "male",
    "personalAttributesMatches": 11
}

/* 3 */
{
    "_id" : ObjectId("5996c6d66f8910361b8232b5"),
    "firstName" : "Eliezer",
    "gender" : "male",
    "personalAttributesMatches": 7
}

對此的見解將不勝感激!!

您可以使用setIntersection表達式,因此您的項目階段如下所示:

"$project": {
    "_id": 1,
    "gender": 1,
    "firstName": 1,
    "profileImage": 1,
    "personalAttributesMatches": {
        $size:{
            $setIntersection: ["$personalAttributes", _other_persons_attributes_]
        }
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM