How to count matched elements from an Array in another Array MongoDB -


i using mongodb aggregate framework query users collection.

this is example of user schema layout below:

{     "_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",         ] } 

this steps outlined in aggregate pipeline.

1: geolocate users within specified distance using $geonear operator.

2: match 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 want pass users personalattributes array , count number of matched items in each array .it 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 users personalattributes array , count number of matches , return data.                  }         }     }   } ]) 

with expected output 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 appreciated!!

you can make use of setintersection expression, project stage can following:

"$project": {     "_id": 1,     "gender": 1,     "firstname": 1,     "profileimage": 1,     "personalattributesmatches": {         $size:{             $setintersection: ["$personalattributes", _other_persons_attributes_]         }     }  } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -