2

I am using the MongoDB Aggregate framework to query a users collection.

This is the is an example of the user schema layout below:

{
    "_id" : ObjectId("XXXXXXXXXXXXXX367db"),
        "updatedAt" : ISODate("2017-08-18T10:59:54.904Z"),
            "createdAt" : ISODate("2017-08-18T10:59:54.904Z"),
                "email" : "[email protected]",
                    "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",
        ]
}

This is steps outlined in the aggregate pipeline.

1: Geolocate users within a specified distance using the $geoNear operator.

2: Match the users based on gender.

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

What I want to do is pass another users personalAttributes Array and count the number of matched items in each array .It would look something like this:

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. 
                }
        }
    }

 }
])

With the expected output to be

/* 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
}

Insights into this would be much appreciated!!

1 Answer 1

3

You can make use of setIntersection expression, so your project stage can look like following:

"$project": {
    "_id": 1,
    "gender": 1,
    "firstName": 1,
    "profileImage": 1,
    "personalAttributesMatches": {
        $size:{
            $setIntersection: ["$personalAttributes", _other_persons_attributes_]
        }
    } 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks I'm going to test it out on the mongoose framework now! Once the I get it working I'll mark your answer as correct. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.