如何在同一查询中检索用户的关注者和我已经关注的用户 mongodb
How to retrieve in the same query user's followers and users I already follow in mongodb
我将 mongodb 与此 collection 一起使用:用户和关注。用户 collection 有用户,关注有关注者映射。
尝试 运行 此查询,但只获取用户的关注者以及我关注的人:
users = User.collection.aggregate([{
"$lookup": {
from: "follows",
localField: "_id",
foreignField: "followable_id",
as: "follow"
}
},
{
"$match": {
"follow.followable_type": "User",
"follow.user_id": BSON::ObjectId.from_string(userid)
}
},
{
"$group": {
_id: "$follow.followable_id",
count: {
"$sum": 1
},
data: {
"$addToSet": "$$ROOT"
},
}
},
{
"$project": {
_id: 1,
data: 1,
count: 1,
follow: {
'$cond': {
'if': {
'$eq': ["$count", 2]
},
then: true,
else: false
}
}
}
},
{
"$skip": 0 * 10
},
{
"$limit": 10
}
])
users.each do |user|
puts "#{user['data'].first['name']}: #{user['follow']}: #{user['count']}"
end
如何 return 在同一查询中我关注的用户?
输出:
Diego_Lukmiller: false: 1
Marianno: false: 1
kah: false: 1
Fausto Torres: false: 1
我solved/fixed分三步完成(写在ruby/mongoid):
//get users the user X follows
users = Follow.collection.aggregate([
{ "$match": { "followable_id": BSON::ObjectId.from_string(userid) } },
{ "$lookup": { from: "users", localField: "user_id", foreignField: "_id", as: "user" } },
{ "$unwind": "$user" },
{ "$sort": {"created_at":-1} },
{ "$skip": page * 10 },
{ "$limit": 10 },
{ "$group":{ _id: "$followable_id", data: {"$addToSet": "$user._id"} } }
])
userids=users.blank? ? [] : users.first['data'].to_a
//get users I follow
f=Follow.collection.aggregate([
{"$match":{user_id: BSON::ObjectId.from_string(meid), followable_id: {"$in":userids}}},
{"$group":{ _id: nil, data: {"$addToSet": "$followable_id"}}}
])
//make intersection
users = User.collection.aggregate([
{ "$lookup": { from: "follows", localField: "_id", foreignField: "user_id", as: "follow" } },
{ "$unwind": "$follow" },
{ "$match": { "follow.followable_type": "User","follow.followable_id": BSON::ObjectId.from_string(userid) } },
{ "$group":{ _id: "$_id", data:{"$addToSet": "$$ROOT"}} },
{ "$unwind": "$data" },
{ "$project": {
_id: 1,data: 1,count: 1,
follow: {
'$cond': {
"if": { '$in': [ "$_id", f.first['data'] ] }, "then": true,
"else": false
}
}
}
},
{ "$skip": page * 10 },
{ "$limit": 10 }
])
输出:
name: Fausto Torres; Do I follow too? false; Counter that's make the magic: 1
name: kah; Do I follow too? false; Counter that's make the magic: 1
name: Marianno; Do I follow too? true; Counter that's make the magic: 2
name: Diego_Lukmiller; Do I follow too? true; Counter that's make the magic: 2
查询执行得非常快并且看起来很简单。 有没有办法/如何在一个查询中解决它?!?
我将 mongodb 与此 collection 一起使用:用户和关注。用户 collection 有用户,关注有关注者映射。
尝试 运行 此查询,但只获取用户的关注者以及我关注的人:
users = User.collection.aggregate([{
"$lookup": {
from: "follows",
localField: "_id",
foreignField: "followable_id",
as: "follow"
}
},
{
"$match": {
"follow.followable_type": "User",
"follow.user_id": BSON::ObjectId.from_string(userid)
}
},
{
"$group": {
_id: "$follow.followable_id",
count: {
"$sum": 1
},
data: {
"$addToSet": "$$ROOT"
},
}
},
{
"$project": {
_id: 1,
data: 1,
count: 1,
follow: {
'$cond': {
'if': {
'$eq': ["$count", 2]
},
then: true,
else: false
}
}
}
},
{
"$skip": 0 * 10
},
{
"$limit": 10
}
])
users.each do |user|
puts "#{user['data'].first['name']}: #{user['follow']}: #{user['count']}"
end
如何 return 在同一查询中我关注的用户?
输出:
Diego_Lukmiller: false: 1
Marianno: false: 1
kah: false: 1
Fausto Torres: false: 1
我solved/fixed分三步完成(写在ruby/mongoid):
//get users the user X follows
users = Follow.collection.aggregate([
{ "$match": { "followable_id": BSON::ObjectId.from_string(userid) } },
{ "$lookup": { from: "users", localField: "user_id", foreignField: "_id", as: "user" } },
{ "$unwind": "$user" },
{ "$sort": {"created_at":-1} },
{ "$skip": page * 10 },
{ "$limit": 10 },
{ "$group":{ _id: "$followable_id", data: {"$addToSet": "$user._id"} } }
])
userids=users.blank? ? [] : users.first['data'].to_a
//get users I follow
f=Follow.collection.aggregate([
{"$match":{user_id: BSON::ObjectId.from_string(meid), followable_id: {"$in":userids}}},
{"$group":{ _id: nil, data: {"$addToSet": "$followable_id"}}}
])
//make intersection
users = User.collection.aggregate([
{ "$lookup": { from: "follows", localField: "_id", foreignField: "user_id", as: "follow" } },
{ "$unwind": "$follow" },
{ "$match": { "follow.followable_type": "User","follow.followable_id": BSON::ObjectId.from_string(userid) } },
{ "$group":{ _id: "$_id", data:{"$addToSet": "$$ROOT"}} },
{ "$unwind": "$data" },
{ "$project": {
_id: 1,data: 1,count: 1,
follow: {
'$cond': {
"if": { '$in': [ "$_id", f.first['data'] ] }, "then": true,
"else": false
}
}
}
},
{ "$skip": page * 10 },
{ "$limit": 10 }
])
输出:
name: Fausto Torres; Do I follow too? false; Counter that's make the magic: 1
name: kah; Do I follow too? false; Counter that's make the magic: 1
name: Marianno; Do I follow too? true; Counter that's make the magic: 2
name: Diego_Lukmiller; Do I follow too? true; Counter that's make the magic: 2
查询执行得非常快并且看起来很简单。 有没有办法/如何在一个查询中解决它?!?