我正在尝试对所有不同的字段进行分组然后查找到另一个表,问题是我在查找中为同一个用户添加了太多结果,所以我想知道如何在内部对这些结果进行分组然后在下一个阶段中,将另一个查找添加到另一个表中。 目前我有以下代码:
Post.aggregate([
{ "$group" : {"_id" : { "id_fanpage": "$id_fanpage" },"total": { $sum: 1 } } },
{
"$lookup": {
"from": "v3_post_reaccion_angry",
"localField": "_id.id_fanpage",
"foreignField": "id_fanpage",
"as": "resultingAngries"
}
},
{
"$lookup": {
"from": "v3_post_reaccion_compartir",
"localField": "_id.id_fanpage",
"foreignField": "id_fanpage",
"as": "resultingCompartir"
}
}
]).exec(function(err, results){
console.log(results);
return results
}).then(success(res))
我要做的是通过“id_fanpage”进行不同的Post行,然后通过返回的id_fanpage加入另一个表,这基本上按预期工作,结果如下:
{
"_id": {
"id_fanpage": "5a63a96d1aa940ca0ada1c82"
},
"total": 831,
"resultingAngries": [
{
"_id": "5a71ccda1aa940dd154caf83",
"id_post": "5a71ccda1aa940dd154caf82",
"id_fanpage": "5a63a96d1aa940ca0ada1c82",
"facebook_usuario_id": 345346565665,
"facebook_usuario_nombre": "AAAAAAAAAAAAAAAAA"
},
{
"_id": "5a71cd6b1aa940dd154ddb43",
"id_post": "5a71cd6b1aa940dd154ddb40",
"id_fanpage": "5a63a96d1aa940ca0ada1c82",
"facebook_usuario_id": "44444444444444444",
"facebook_usuario_nombre": "XXXXXXXXXXXXXXXX"
},
{
"_id": "5a71d7f61aa940dd156b60f4",
"id_post": "5a71d7f61aa940dd156b60e9",
"id_fanpage": "5a63a96d1aa940ca0ada1c82",
"facebook_usuario_id": "asdf12345232421",
"facebook_usuario_nombre": "YYYYYYYYYYYYYY"
},
{
"_id": "5a71cde11aa940dd154ec514",
"id_post": "5a71cde11aa940dd154ec513",
"id_fanpage": "5a63a96d1aa940ca0ada1c82",
"facebook_usuario_id": "asdf2345234555",
"facebook_usuario_nombre": "ZZZZZZZZZZZZZZ"
},
{
"_id": "5a71cdcb1aa940dd154e917a",
"id_post": "5a71cdcb1aa940dd154e9178",
"id_fanpage": "5a63a96d1aa940ca0ada1c82",
"facebook_usuario_id": "44444444444444444",
"facebook_usuario_nombre": "XXXXXXXXXXXXXXX"
}
]
}
现在的问题是,我想通过facebook_usuario_id对resultsAngries中的所有结果进行分组,并且能够将相同的结果添加到下一个查找中,但据我所知,我不能在第一次查找后使用组而不影响下次查找。 所以结果应该是:
{
"_id": {
"id_fanpage": "5a63a96d1aa940ca0ada1c82"
},
"total": 831,
"resultingAngries": [
{
"_id": "this is my user id 1111111111",
"count": N,
},
{
"_id": "this is my user id 2222222222",
"count": Y,
}
],
"resultingLikes": [
{
"_id": "this is my user id 1111111111",
"count": N,
},
{
"_id": "this is my user id 2222222222",
"count": Y,
}
],
},
{
"_id": {
"id_fanpage": "5a63a96d1aa942345wedf23455s"
},
"total": 20,
"resultingLikes": [
{
"_id": "this is my user id 1111111111",
"count": N,
},
{
"_id": "this is my user id 2222222222",
"count": Y,
}
],
},
"resultingAngries": [
{
"_id": "this is my user id 1111111111",
"count": N,
},
{
"_id": "this is my user id 2222222222",
"count": Y,
}
],
等等。 我已经看过聚合,查找并且不确定$ addToSet是否可以实现它,因为到目前为止我无法进行内部分组,因此应用addToSet给我带来了困难。帮助将是欣赏。
答案 0 :(得分:0)
在$ lookup&#39>之间插入以下三个阶段。
以下步骤$unwind
resultingAngries
数组后跟第一个$group
来计算facebook_usuario_id
&的独特组合id_fanpage
和第二$group
$push
count
和facebook_usuario_id
返回resultingAngries
数组。{/ p>
{"$unwind":"$resultingAngries"},
{"$group":{
"_id":{
"id_fanpage":"$_id.id_fanpage",
"facebook_usuario_id":"$resultingAngries.facebook_usuario_id"
},
"total":{"$first":"$total"},
"count":{"$sum":1}
}},
{"$group":{
"_id":{"id_fanpage":"$_id.id_fanpage"},
"total":{"$first":"$total"},
"resultingAngries":{
"$push":{
"_id":"$_id.facebook_usuario_id",
"count":"$count"
}
}
}}