我所拥有的只是一个userid和这个查询:
return gamelog.find({userid: userid).then(function (response) {
return response;
});采用以下模式:
new Schema({
userid : String,
ip: String,
});结果至少会给我一个或多个IP。然后,我需要查询那些IP,以检查是否有其他用户在该IP地址上。
我如何修改这个查询,从而从其中一个IP中获得带有该userid或任何userid的所有IP?
理想的结果将导致所有的用户的IP地址,以及所有其他的用户已经在任何一个IP的用户标识已经打开。
编辑:
样本文件:
{
time: "1507127113173",
ip:"::1",
others: 0,
url:"/api/gameapi?action=getweapon",
userid:"59d4ef435048380ff4abd683"
}
{
time: "1507127113173",
ip:"::1",
others: 0,
url:"/api/gameapi?action=getweapon",
userid:"59d4ef43504833333333380ff4abd683"
}发布于 2017-10-18 22:07:39
您可以尝试在聚合下面。
下面的查询将过滤"userid“的所有文档,然后是$group以获取" ips”的集合,并将$lookup加入到所有与查询用户共享相同ips的用户中。
$unwind其他用户的文档和$group对其他用户的id和收集ips,然后最后一组,以获得用户id和ips的集合。
db.gamelog.aggregate([
{
"$match": {
"userid": userId
}
},
{
"$group": {
"_id": null,
"ips": {
"$push": "$ip"
}
}
},
{
"$lookup": {
"from": "gamelog",
"localField": "ips",
"foreignField": "ip",
"as": "otherusers"
}
},
{
"$unwind": "$otherusers"
},
{
"$match": {
"otherusers.userid": {
"$ne": userId
}
}
},
{
"$group": {
"_id": "$otherusers.userid",
"ips": {
"$first": "$ips"
},
"otheruserips": {
"$push": "$otherusers.ip"
}
}
},
{
"$group": {
"_id": null,
"ips": {
"$first": "$ips"
},
"otherusers": {
"$push": {
"userid": "$_id",
"ips": "$otheruserips"
}
}
}
}
])https://stackoverflow.com/questions/46818528
复制相似问题