简体   繁体   中英

inner join using mongo query (pymongo)

I am new to SQL query and first time working with MongoDB using Pymongo.

I have two collections in 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'

i want to inner join these 2 query and return all matching record and give all details from DEPARTMENT table and emp_name, emp_id from EMPLOYEE table.

how will i achieve it using pymongo and sql query.

Any help will be greatly appreciated!

Thanks in advance!

Try this:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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