繁体   English   中英

使用 mongo 查询(pymongo)的内部连接

[英]inner join using mongo query (pymongo)

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

我在 MongoDB 中有两个 collections。

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 表和 EMPLOYEE 表中的 emp_name、emp_id 中提供所有详细信息。

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

任何帮助将不胜感激!

提前致谢!

尝试这个:

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'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM