mongodb -如何从一个集合中获得排序的共同喜欢列表?

osh3o9ms  于 2023-01-25  发布在  Go
关注(0)|答案(1)|浏览(86)

我有两个系列:users-所有用户信息'partnership'-用户可以合作/跟随对方。

**目标:**按最后合作日期对所有合作关系进行排序。
**用户何时成为合作伙伴:**假设user_1喜欢user_2,并且仅当user_2也喜欢user_1时,他们才成为合作伙伴(相互喜欢)
**什么是合作日期:**第二个喜欢(相互喜欢)发生的日期/时间。

我创建了一个mongoPlayground,但请忽略我的查询-Mongo Playground

这是我的示例数据

db={
  partnership: [
    {
      _id: "xyz_rrr",
      updated: "2022-10-23T12:35:24.772+00:00",
      users: [
        "xyz",
        "rrr"
      ]
    },
    {
      _id: "rrr_eee",
      updated: "2022-12-23T12:35:24.772+00:00",
      users: [
        "rrr",
        "eee"
      ]
    },
    {
      _id: "eee_rrr",
      updated: "2023-01-21T12:35:24.772+00:00",
      users: [
        "eee",
        "rrr"
      ]
    },
    {
      _id: "mmm_rrr",
      updated: "2023-02-19T12:35:24.772+00:00",
      users: [
        "mmm",
        "rrr"
      ]
    },
    {
      _id: "rrr_mmm",
      updated: "2023-02-21T12:35:24.772+00:00",
      users: [
        "rrr",
        "mmm"
      ]
    },
    
  ],
  users: [
    {
      _id: "abc",
      name: "abc",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          53.23,
          67.12
        ]
      },
      calculatedDist: 112
    },
    {
      _id: "xyz",
      name: "xyyy",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          54.23,
          67.12
        ]
      },
      calculatedDist: 13
    },
    {
      _id: "123",
      name: "yyy",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          54.23,
          67.12
        ]
      },
      calculatedDist: 13
    },
    {
      _id: "rrr",
      name: "rrrrrrr",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          51.23,
          64.12
        ]
      },
      calculatedDist: 14
    },
    {
      _id: "mmm",
      name: "mmmm",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          51.23,
          64.12
        ]
      },
      calculatedDist: 14
    },
    {
      _id: "eee",
      name: "eeeee",
      group: 1,
      location: {
        type: "Point",
        coordinates: [
          55.23,
          62.12
        ]
      },
      calculatedDist: 143
    }
  ],
  
}

预期成果

{
partneredUsers:
{ firstUser :
     {
      _id: "mmm",
      name: "mmmm",
    },
},
secondUser :
    {
      _id: "rrr",
      name: "rrrrrrr",
      
    },
},
partneredDate: "2023-02-21T12:35:24.772+00:00",
},

{
partneredUsers:
{ firstUser :
    {
      _id: "rrr",
      name: "rrrrrrr",
      
    },
},
secondUser :
    {
      _id: "eee",
      name: "eeeee",
      
    }
},
partneredDate: "2023-01-23T12:35:24.772+00:00",
}
}
wmomyfyw

wmomyfyw1#

partnership集合开始,通过$sortArray$concat创建一个分区键来标识彼此喜欢的关系(他们将共享同一个键),使用$setWindowFields中的分区键来计算计数和排名。

  • count:当count〉1时互相喜欢的用户对
  • 排名:排序更新:-1;最新的类似将具有等级:1

最后$lookupusers得到用户的详细信息,$sort通过partneredDate得到。

db.partnership.aggregate([
  {
    $set: {
      partitionKey: {
        "$reduce": {
          "input": {
            $sortArray: {
              input: "$users",
              sortBy: 1
            }
          },
          "initialValue": "",
          "in": {
            "$concat": [
              "$$value",
              "$$this"
            ]
          }
        }
      }
    }
  },
  {
    "$setWindowFields": {
      "partitionBy": "$partitionKey",
      "sortBy": {
        "updated": -1
      },
      "output": {
        "count": {
          $sum: 1
        },
        rank: {
          $rank: {}
        }
      }
    }
  },
  {
    $match: {
      count: {
        $gt: 1
      },
      rank: 1
    }
  },
  {
    "$lookup": {
      "from": "users",
      "localField": "users",
      "foreignField": "_id",
      "as": "userLookup"
    }
  },
  {
    "$project": {
      _id: 0,
      partneredUsers: {
        firstUser: {
          $first: "$userLookup"
        },
        secondUser: {
          $last: "$userLookup"
        }
      },
      partneredDate: "$updated"
    }
  },
  {
    $sort: {
      partneredDate: -1
    }
  }
])

Mongo Playground

相关问题