I have this query:
SELECT DISTINCT 1 as table_id, users.id, users.username, a.titles_name title1, b.titles_name as title2, contacts.accepted
FROM users
LEFT JOIN contacts ON users.id = contacts.contact_id
LEFT JOIN titles as a ON a.id = users.title_1
LEFT JOIN titles as b ON b.id = users.title_2
WHERE contacts.request_id = ' + $this->session->userdata('user_id') . '
UNION DISTINCT
SELECT DISTINCT 2 as table_id, users.id, users.username, a.titles_name title1, b.titles_name as title2, contacts.accepted
FROM users
LEFT JOIN contacts ON users.id = contacts.request_id
LEFT JOIN titles as a ON a.id = users.title_1
LEFT JOIN titles as b ON b.id = users.title_2
WHERE contact_id = ' . $this->session->userdata('user_id')
Would it be possible to do something similar in mongodb?
Lets break down the components of the query:
Yes, this is just a distinct query asking for a subset of fields. Mongo does that.
This is a join, which you cannot do in mongo. However, you can de-normalize so it becomes unnecessary or do similar joining of data on the client side.
This is a union, which you could do on the client side and for which the query contains the same components i’ve described above.
TLDNR: No, you can’t do this exact query in mongodb, but you can achieve the same result.