【问题标题】:How do I perform a mongoDB lookup and then merge results into a single data set?如何执行 mongoDB 查找,然后将结果合并到单个数据集中?
【发布时间】:2021-09-27 23:02:03
【问题描述】:

您好,我正在寻找连接两个表中的数据

我要做的是使用查找在两个用户之间创建“朋友”关系,并找出每个朋友有多少电子邮件(如果发送了)

这里是一个 mongo DB 游乐场...我很接近,我只是不知道如何获取“其他用户”的电子邮件信息

我应该只看到来自 a 的朋友(b 和 c)的电子邮件,而不是从 a 发送的任何电子邮件

这个游乐场几乎做我想做的...

https://mongoplayground.net/p/KKocPm3fzEv

这是上述操场的输入

db={
  "users": [
    {
      "_id": "a",
      "email": "a@test.com",
    },
    {
      "_id": "b",
      "email": "b@test.com",
    },
    {
      "_id": "c",
      "email": "c@test.com",
    }
  ],
  "friends": [
    {
      "userId": "a",
      "otherUserId": "b"
    },
    {
      "userId": "a",
      "otherUserId": "c"
    },
  ],
  "emailsSent": [
    {
      "userId": "a",
      "number": "25"
    },
    {
      "userId": "b",
      "number": "3"
    },
  ]
}

这是上述操场的输出

[
  {
    "_id": "a",
    "a_myfriends": [
      {
        "_id": ObjectId("5a934e000102030405000002"),
        "otherUserId": "b",
        "userId": "a"
      },
      {
        "_id": ObjectId("5a934e000102030405000003"),
        "otherUserId": "c",
        "userId": "a"
      }
    ],
    "email": "a@test.com",
    "emailaddr": [
      {
        "_id": "b",
        "email": "b@test.com"
      },
      {
        "_id": "c",
        "email": "c@test.com"
      }
    ],
    "emailsent": [
      {
        "_id": ObjectId("5a934e000102030405000001"),
        "number": "3",
        "userId": "b"
      }
    ]
  }
]

现在有三个信息数组...我如何将它们全部连接在一起,以便数组中的每个条目仅用于“那个”朋友?

这就是我想要的结果

{
  "_id": "a",
  "a_myfriends": [
    {
      "otherUserId": "b",
      "email": "b@test.com",
      "number": "3"
    },
    {
      "otherUserId": "c",
      "email": "c@test.com"
    }
  ]
}

注意:我尝试连接本文中的联合,但由于用户 ID 的差异(例如 _id 和 userId),我认为它不起作用

MongoDB: Combine data from multiple collections into one..how?

【问题讨论】:

    标签: mongodb lookup


    【解决方案1】:

    我认为你可以减少集合的数量 => 减少 $lookups 并且数据看起来会更简单

    这个 1 个集合的架构怎么样?
    即使某人有太多朋友,比如 200.000,你也可以有一个额外的字段{"extra_friends" "_id"},并且很少使用第二个朋友收藏,很少最多到 2 个收藏。

    users=
    [
        {
          "_id": "a",
          "email": "a@test.com",
          "emails-send" 25,
          "friends" ["b" "c"]      
        },
        {
          "_id": "b",
          "email": "b@test.com",
          "emails-send" 3,
          "friends" [....]
        },
        {
          "_id": "c",
          "email": "c@test.com",
          "emails-send" 0
          "friends" [....]
        }
      ]
    

    查询
    (对于您的架构,产生预期的数据)

    • 一系列$lookup$unwind
    • 来自用户 - 给朋友(交朋友) - 给用户(获取朋友的电子邮件) -to emailsS​​ent(获取已发送邮件的数量)
    • 中间有一些 $set 以保持架构简单
    • group by _id back 并结合这些朋友

    *查询可能会变小一点,但这样很容易理解,逐步了解发生了什么

    Test code here

    db.users.aggregate([
      {
        "$match": {
          "$expr": {
            "$eq": [
              "$_id",
              "a"
            ]
          }
        }
      },
      {
        "$lookup": {
          "from": "friends",
          "localField": "_id",
          "foreignField": "userId",
          "as": "myfriends"
        }
      },
      {
        "$unwind": {
          "path": "$myfriends"
        }
      },
      {
        "$lookup": {
          "from": "users",
          "localField": "myfriends.otherUserId",
          "foreignField": "_id",
          "as": "myfriendsEmails"
        }
      },
      {
        "$unwind": {
          "path": "$myfriendsEmails"
        }
      },
      {
        "$set": {
          "myfriends.email": "$myfriendsEmails.email"
        }
      },
      {
        "$unset": [
          "myfriendsEmails",
          "myfriends._id",
          "myfriends.userId"
        ]
      },
      {
        "$lookup": {
          "from": "emailsSent",
          "localField": "myfriends.otherUserId",
          "foreignField": "userId",
          "as": "friendsEmails"
        }
      },
      {
        "$unwind": {
          "path": "$friendsEmails",
          "preserveNullAndEmptyArrays": true
        }
      },
      {
        "$set": {
          "myfriends.number": "$friendsEmails.number"
        }
      },
      {
        "$unset": [
          "friendsEmails"
        ]
      },
      {
        "$group": {
          "_id": "$_id",
          "email": {
            "$first": "$email"
          },
          "friends": {
            "$push": "$myfriends"
          }
        }
      }
    ])
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多