【问题标题】:Lookup in mongo DB在 mongoDB 中查找
【发布时间】:2020-08-04 23:55:10
【问题描述】:

我正在尝试使用 mongoDB 为自定义博客网站建模数据库。我有以下收藏

用户收藏:

[{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "first_name": "xxx1",
  "last_name": "xxx1",
  "email": "xxx@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef74"
  },
  "first_name": "xxx2",
  "last_name": "xxx2",
  "email": "xxx2@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef75"
  },
  "first_name": "xxx3",
  "last_name": "xxx3",
  "email": "xxx3@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef76"
  },
  "first_name": "xxx4",
  "last_name": "xxx4",
  "email": "xxx4@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef77"
  },
  "first_name": "xxx3",
  "last_name": "xxx3",
  "email": "xxx3@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef78"
  },
  "first_name": "xxx4",
  "last_name": "xxx4",
  "email": "xxx4@gmail.com"
}]

帖子集合:我创建了名为“评论和喜欢”的嵌入文档,并使用了来自用户集合的 user_id 参考字段。

[{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f6"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_date": {
    "$date": "2020-07-30T20:22:48.839Z"
  },
  "post_content": "Content 1",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      }
    }
  ]
},{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f7"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_date": {
    "$date": "2020-07-30T20:22:48.839Z"
  },
  "post_content": "content 2",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      }
    }
  ]
}]

渴望输出:

 {
    "_id": {
        "$oid": "5f232c18a93ce29203b3f2f6"
    },
    "post_created_user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
    },
    "post_date": {
        "$date": "2020-07-30T20:22:48.839Z"
    },
    "post_content": "Content 1",
      "post_likes": [{
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef73"
        },
        user_info : {
            "first_name": "xxx1",
            "last_name": "xxx1",
            "email": "xxx@gmail.com"
        
    }, {
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef74"
        },
        user_info : {
            "first_name": "xxx2",
            "last_name": "xxx2",
            "email": "xxx2@gmail.com"
        
    }],
    "post_comments": [{
        "comment_id": {
            "$oid": "5f2850966efb9d4e803b2030"
        },
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "comment_content": "Test comment1",
        "comment_date": {
            "$date": "2020-08-03T17:59:50.132Z"
        },
        user_info : {
            "first_name": "xxx1",
            "last_name": "xxx1",
            "email": "xxx@gmail.com"
        }
    }, {
        "comment_id": {
            "$oid": "5f2850ff6efb9d4e803b2033"
        },
        "user_id": {
            "$oid": "5ed15483f43c335cb2e9ef74"
        },
        "comment_content": "test comments 1",
        "comment_date": {
            "$date": "2020-08-03T18:01:35.008Z"
        }
        user_info : {
            "first_name": "xxx2",
            "last_name": "xxx2",
            "email": "xxx2@gmail.com"
        }
    }]
}

查找我尝试过的帖子集合:

[
    {
        '$unwind': {
            'path': '$post_likes', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$unwind': {
            'path': '$post_comments', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$lookup': {
            'from': 'users_details', 
            'localField': 'post_likes.user_id', 
            'foreignField': '_id', 
            'as': 'post_likes.user_info'
        }
    }, {
        '$lookup': {
            'from': 'users_details', 
            'localField': 'post_comments.user_id', 
            'foreignField': '_id', 
            'as': 'post_comments.user_info'
        }
    }, {
        '$unwind': {
            'path': '$post_likes.user_info', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$unwind': {
            'path': '$post_comments.user_info', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$group': {
            '_id': '$_id', 
            'post_created_user_id': {
                '$first': '$post_created_user_id'
            }, 
            'post_content': {
                '$first': '$post_content'
            }, 
            'post_likes': {
                '$push': '$post_likes'
            }, 
            'post_comments': {
                '$push': '$post_comments'
            }
        }
    }
]

使用重复的 val 获取以下输出。

[{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f6"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_content": "Content 1",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef75"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef75"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef76"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef76"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    }
  ]
},{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f7"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_content": "content 2",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef77"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef77"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef78"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef78"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    }
  ]
}]

有人可以帮我构建聚合吗?

【问题讨论】:

  • 这是我使用的查找查询。

标签: python mongodb


【解决方案1】:

一步一步看,

post_cmets 查找

  • $unwind post_cmets 因为它的数组,我们需要通过单个评论查找
db.posts.aggregate([
  {
    $unwind: "$post_comments"
  },
  • $lookup你准备的没错
  {
    $lookup: {
      from: "users_details",
      localField: "post_comments.user_id",
      foreignField: "_id",
      as: "post_comments.user_info"
    }
  },
  • 从 user_info 中删除了不需要的字段
  {
    $project: {
      "post_comments.user_info._id": 0
    }
  },
  • $unwind post_comments.user_info 因为它是一个数组,我们只需要一个 user_info 对象
  {
    $unwind: {
      path: "$post_comments.user_info"
    }
  },
  • $group by _id 因为上面的 unwind 把所有文件都分开了,我们这里需要合并
  {
    $group: {
      _id: "$_id",
      post_created_user_id: {
        $first: "$post_created_user_id"
      },
      post_date: {
        $first: "$post_date"
      },
      post_content: {
        $first: "$post_content"
      },
      post_comments: {
        $push: "$post_comments"
      }
    }
  }
])

post_likes 查找

  • 重复上述步骤只需将post_comments更改为post_likes并在$group中更改为推送post_likes,您可以查看下面的操场。

上面的查询部分可以按顺序组合起来,为了便于说明而分开。

游乐场:https://mongoplayground.net/p/GzVvEbPNwkA

【讨论】:

  • 谢谢哥们。它的工作。所以我需要在 unwiind 使用 droup by 之后推动所有领域。这是一个很好的学习。
  • 我还有一个问题。在 post 文档中,我有 post_like 字段,它也是 User 的参考字段。当我对 post_like 进行同样的聚合时,它会返回与我发表评论相同数量的数组。例如,假设有 3 个用户在帖子中发表评论,并且不同的用户喜欢该帖子。在这种情况下,我该如何编写聚合代码?
  • 这里是它的链接。 mongoplayground.net/p/SGROpec1EON
  • 所以如果我把它改成 like_user_id ,它会工作吗?
  • 我做错了顺序。不管怎么说,还是要谢谢你。有没有办法按日期缩短 post_cmets 数组?
猜你喜欢
  • 2018-02-09
  • 1970-01-01
  • 2018-08-31
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
相关资源
最近更新 更多