【发布时间】:2020-06-30 03:00:11
【问题描述】:
我想获得相互(因此从所有用户中,我拥有最多共享关注者的用户(我们都关注同一个用户)按该计数排序)。来自我的数据库方案 (mongoDB) 的关注者,具有聚合功能。
用户方案:
let User = new mongoose.Schema({
uid: {
type: String,
require: true,
unique: true
},
followers: [String],
following: [String]
})
followers/following 是一组 UID
我试过了:
let me = await User.findOne({uid: uid})
if (me) {
console.log(me.followers)
let mutual = await User.aggregate([
// Match documents that contain the elements
{ "$match": {
"followers": { "$in": me.followers }
}},
// // De-normalize the array field content
{ "$unwind": "$followers" },
// Match just the elements you want
{ "$match": {
"followers": { "$in": me.followers }
}},
// Count by the element as a key
{ "$group": {
"_id": "$name",
"mutual": { "$sum": 1}
}}
])
if (mutual) {
console.log('mutual', mutual)
}
}
但这并没有给出正确的计数
【问题讨论】:
-
看起来该查询将找到所有关注原始
uid的人也关注的人。通过“共同关注者”,您的意思是要找到所有关注uid和uid也关注的人?或者您是否正在寻找也关注uid关注的人的人? -
问题不完整。你能定义一下你所说的相互吗?