【问题标题】:Laravel 7 how to perform a query sql with join in mongoDBLaravel 7 如何在 mongoDB 中使用 join 执行查询 sql
【发布时间】:2020-10-03 04:23:46
【问题描述】:

我的项目中有 2 个数据库(一个在 mySql 中,另一个在 MongoDB 中),我需要执行这样的查询

public function getPosts() {
    $user = Auth::user();
    $follow = DB::connection("mongodb")
    ->collection("followers")
    ->where("user_id", $user->id)->get();
    /*$res = DB::select("
    SELECT * 
    FROM posts 
    WHERE user_id = ? OR user_id IN 
        (SELECT user_id from $follows) 
    ORDER BY created_at DESC", [$user->id, $user->id]);

*/
    return response()->json($res);
}

这是一个查询,它返回来自登录用户和用户关注的人的帖子 关注者表(MongoDB 中的那个)包含“user_id”和“follows_id” 注释行是原始查询(mySql 上单个数据库中的每个表


谢谢


编辑:我通过 mongodb 中的查询解决了问题,然后我编辑了结果以获取我通过 orWhereIn 合并到 sql 查询中的数组
谢谢您的回答 :)

【问题讨论】:

  • MongoDB 使用 NoSQL,“join”这个词在这种语言中没有意义。
  • 有没有办法在两个数据库之间执行?

标签: mysql database laravel mongodb


【解决方案1】:

我不这么认为...您可以尝试真正的多模型数据库,例如 Oracle XE(它是免费的)来实现您的目标...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多