MongDB中的聚集集合

时间:2018-09-20 09:12:13

标签: mongodb aggregation-framework

如何在monogdb中的汇总查询结果中填充 followedId的数组

var followeduserId = ["abc","efg","xyz","pqr","acd","rts"];

推荐的饲料

[
  {
    "feedsId": "feed1",
    "userId": "abc"
  },
  {
    "feedsId": "feed1",
    "userId": "efg"
  }
]

收款

[
  {
    "link": "www.yodo.com",
    "recommended": [
      "abc",
      "efg"
    ],
    "title": "This is my feed7",
    "topics": [
      "topi1",
      "topi2",
      "topi3",
      "topi4"
    ]
  },
  {
    "link": "www.yodo.com",
    "recommended": [
      "abc",
      "efg",
      "das",
      "asd",
      "eqw",
      "weq"
    ],
    "title": "This is my feed8",
    "topics": [
      "topi1",
      "topi2",
      "topi3",
      "topi4"
    ]
  }
]

随机聚集查询

feedsrecommended.aggregate([
  { $match: { userId: { $in: "followersId" }}},
  { $lookup: {
    from: "feeds",
    localField: "feedsId",
    foreignField: "_id",
    as: "feedsId"
  }},
  { $group: {
    "_id": { "feedsId": "$feedsId" },
    "count": { "$sum": 1 }
  }},
  { $sort: { count: -1 }}
])

汇总后的结果

var resultfeeds = [
  {
    "count": 7,
    "id": {
      "_id": "feed1",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "efg",
        "xyz",
        "pqr",
        "acd",
        "rts"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi1",
        "topi8",
        "topi6",
        "topi5"
      ]
    }
  },
  {
    "count": 3,
    "id": {
      "_id": "feed5",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "efg",
        "acd",
        "rts"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi1",
        "topi2",
        "topi3",
        "topi4"
      ]
    }
  },
  {
    "count": 3,
    "id": {
      "_id": "feed6",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "efg",
        "xyz",
        "pqr"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi7",
        "topi1",
        "topi4",
        "topi8"
      ]
    }
  },
  {
    "count": 2,
    "id": {
      "_id": "feed2",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "acd",
        "rts"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi7",
        "topi6",
        "topi8"
      ]
    }
  },
  {
    "count": 2,
    "id": {
      "_id": "feed7",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "efg"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi1",
        "topi5",
        "topi6",
        "topi4"
      ]
    }
  },
  {
    "count": 1,
    "id": {
      "_id": "feed3",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "asd",
        "eqw",
        "weq"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi1",
        "topi7",
        "topi6",
        "topi4"
      ]
    }
  },
  {
    "count": 1,
    "id": {
      "_id": "feed8",
      "link": "www.yodo.com",
      "recommended": [
        "abc",
        "das",
        "asd",
        "eqw",
        "weq"
      ],
      "title": "This is my feed1",
      "topics": [
        "topi1",
        "topi2",
        "topi5",
        "topi4"
      ]
    }
  }
]

我想在结果中填充主题并推荐用户名和图像

主题收藏

[
  {
    "topic_name": "tiger"
  },
  {
    "topic_name": "loin"
  }
]

用户集合

[
  {
    "name": "deepa",
    "profileImg": "www.com/facebook.jpg"
  },
  {
    "name": "nisa",
    "profileImg": "www.com/facebook.jpg"
  }
]

我的最后结果应该是这样

[
  {
    "count": 2,
    "id": {
      "_id": "feed2",
      "link": "www.yodo.com",
      "recommended": [
        {
          "_id": "abc",
          "name": "deepa",
          "profileImg": "www.com/facebook.jpg"
        },
        {
          "_id": "acd",
          "name": "sigger",
          "profileImg": "www.com/facebook.jpg"
        },
        {
          "_id": "rts",
          "name": "buster",
          "profileImg": "www.com/facebook.jpg"
        }
      ],
      "title": "This is my feed1",
      "topics": [
        {
          "_id": "topi6",
          "topic_name": "boolena"
        },
        {
          "_id": "topi7",
          "topic_name": "mika"
        },
        {
          "_id": "topi8",
          "topic_name": "tika"
        }
      ]
    }
  }
]

1 个答案:

答案 0 :(得分:1)

您可以在mongodb 3.6 及更高版本

中尝试以下聚合
Feedsrecommended.aggregate([
  { "$match": { "userId":{ "$in": followersId }}},
  { "$group": {
    "_id": "$feedsId",
    "count": { "$sum": 1 }
  }},
  { "$lookup": {
    "from": "feeds",
    "let": { "feedsId": "$_id" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": [ "$_id", "$$feedsId" ] }}},
      { "$lookup": {
        "from": "topics",
        "let": { "topics": "$topics" },
        "pipeline": [
          { "$match": { "$expr": { "$in": [ "$_id", "$$topics" ] } } }
        ],
        "as": "topics"
      }},
      { "$lookup": {
        "from": "users",
        "let": { "recommended": "$recommended" },
        "pipeline": [
          { "$match": { "$expr": { "$in": [ "$_id", "$$recommended" ] } } }
        ],
        "as": "recommended"
      }}
    ],
    "as": "feedsId"
  }}
])