【问题标题】:inner join using mongo query (pymongo)使用 mongo 查询(pymongo)的内部连接
【发布时间】:2020-08-05 04:40:15
【问题描述】:

我是 SQL 查询新手,第一次使用 Pymongo 使用 MongoDB。

我在 MongoDB 中有两个集合。

DEPARTMENT

dept_id   dept_name   status    location     
------------------------------------------
123       sales       active     New York
248       IT          inactive   Vermont
845       HR          active     LA

EMPLOYEE

dept_id   emp_name   emp_salary  emp_status  emp_id
----------------------------------------------------
123       John       25000       active      xyz
845       Mary       90000       active      abc
248       Kevin      50000       inactive    qrs

query 1

select * from DEPARTMENT where dept_id=123 and status='active'

query 2

select emp_name, emp_id from EMPLOYEE where dept_id =123 and status = 'active'

我想内部加入这 2 个查询并返回所有匹配的记录,并提供 DEPARTMENT 表和 emp_name、emp_id 表中的所有详细信息。

我将如何使用 pymongo 和 sql 查询来实现它。

任何帮助将不胜感激!

提前致谢!

【问题讨论】:

    标签: python sql mongodb pymongo


    【解决方案1】:

    试试这个:

    SELECT
        EMPLOYEE.emp_name,
        EMPLOYEE.emp_id,
        DEPARTMENT.*
    FROM
        EMPLOYEE LEFT JOIN DEPARTMENT ON EMPLOYEE.dept_id = DEPARTMENT.dept_id
    WHERE
        EMPLOYEE.dept_id = 123
        AND EMPLOYEE.status = 'active'
        AND DEPARTMENT.status = 'active'
    

    【讨论】:

    • 谢谢,但最重要的是我希望它作为 MongoDB 查询
    猜你喜欢
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2015-09-29
    相关资源
    最近更新 更多