Mongodb Aggregate:将具有相似标签的用户分组在一起

时间:2019-06-11 11:37:06

标签: mongodb-query aggregation-framework

我有一个用户集合,文档示例如下

{
"tags" : [ 
    {
        "title" : "Music Lover",
        "createdAt" : 1551260246829.0,
        "updatedAt" : 1551260246829.0,
        "id" : "5c765a56b84f7a23b86c01b9"
    }, 
    {
        "title" : "Coffee",
        "createdAt" : 1550577843788.0,
        "updatedAt" : 1550577843788.0,
        "id" : "5c6bf0b361a5f3278d802868"
    }, 
    {
        "title" : "Football",
        "createdAt" : 1550577855649.0,
        "updatedAt" : 1550577855649.0,
        "id" : "5c6bf0bf61a5f3278d802869"
    }, 
    {
        "title" : "Arts",
        "createdAt" : 1550577863515.0,
        "updatedAt" : 1550577863515.0,
        "id" : "5c6bf0c761a5f3278d80286b"
    }, 
    {
        "title" : "Technology",
        "createdAt" : 1550577869811.0,
        "updatedAt" : 1550577869811.0,
        "id" : "5c6bf0cd61a5f3278d80286c"
    }
],
"first_name" : "Asad",
"last_name" : "Khan",
"location" : {
    "type" : "Point",
    "coordinates" : [ 
        67.0431335, 
        24.8249635
    ]
},
"location_update_time" : "2019-05-31 11:28:00",
 }

位置键是2dsphere。

我需要的是将多个彼此靠近的用户分组在一起,比如说100m并具有通用标签。

到目前为止,我已经可以达到

db.getCollection('user').aggregate([
  {
    "$geoNear": {
    near: { type: "Point", coordinates: [ 67.044535 , 24.825852 ] },
    distanceField: "dist.calculated",
    maxDistance: 2000,
    spherical: true
 }
  },
  { "$unwind": "$tags" },  
  { "$group": { "_id": "$tags.title", "count": { "$sum": 1 }, users: { $push : "$$ROOT" } }},
  {
    "$match": {
        "count": { "$gte": 2 }
     }
  }
 ])

它给了我标签,数量和拥有它们的用户。问题在于,它给所有标签分开了,并且用户在每个标签下重复。而且,仍然无法在查询中添加一些内容来对彼此之间小于100m的用户进行分组。

0 个答案:

没有答案