0

私はMongoDB集計フレームワークを使用してユーザーコレクションを照会しています。他の配列の配列から一致する要素を数える方法MongoDB

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

これは凝集パイプラインで説明する手順である:

これは、以下のユーザスキーマのレイアウトの一例です。

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" 
     } 
    }, 
    { 
     "$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. 
       } 
     } 
    } 

} 
]) 

:私は何をしたいことは、別のユーザーpersonalAttributesの配列を渡すと.ITは次のようになり、各列にマッチしたアイテムの数を数えている

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

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

これについての洞察は非常に高く評価されます。

答えて

1

あなたはsetIntersection表現を利用することができますので、あなたのプロジェクトのステージは、次のようになります。私は今、マングースフレームワークにそれをテストするつもりです

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

大感謝を!私がそれを働かせたら、あなたの答えを正しいものとしてマークします。再度、感謝します! – Dave

+0

ありがとう、アレックス!その作品は完璧! – Dave