How do I perform the SQL Join equivalent in MongoDB

0 votes

How do I use MongoDB to perform the SQL Join equivalent?

If you have two collections (users and comments), let's suppose that I want to retrieve all the comments with pid=444 and the user information for each.

comments
  { uid:12345, pid:444, comment="blah" }
  { uid:12345, pid:888, comment="asdf" }
  { uid:99999, pid:444, comment="qwer" }

users
  { uid:12345, name:"john" }
  { uid:99999, name:"mia"  }

Is it possible to gather all the comments with a specific field (like...find(pid:444)) and the user details connected to each remark at once?

I now gather the comments that meet my requirements first, then I identify all the uids in that result set, obtain the user objects, and combine them with the results of the comments. Things seem like I'm doing it incorrectly.

Sep 13, 2022 in Database by Kithuzzz
• 38,020 points
1,286 views

1 answer to this question.

0 votes

The majority of the responses to this question are incorrect as of Mongo 3.2. A left outer join and the new $lookup operator that was added to the aggregate pipeline are nearly identical:

https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup

From the docs:

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}

Of course, Mongo is not a relational database, and the developers are cautious in their recommendations for $lookup's use cases, but at least as of version 3.2, joining is now supported by MongoDB.

To know more about Mongodb, it's recommended to join Mongodb Certification today.

answered Sep 15, 2022 by narikkadan
• 63,680 points

Related Questions In Database

0 votes
1 answer

How do I perform an IF THEN statement in an SQL SELECT?

The CASE statement is the closest to IF in ...READ MORE

answered Feb 16, 2022 in Database by Vaani
• 7,070 points
1,633 views
0 votes
0 answers

How do I view the Explain Plan in Oracle Sql developer?

I want to check the query execution ...READ MORE

Aug 19, 2022 in Database by Kithuzzz
• 38,020 points
1,315 views
0 votes
1 answer

How do I kill all the processes in Mysql “show processlist”?

Hello kartik, Mass killing operation saves time. Do it ...READ MORE

answered May 4, 2020 in Database by Niroj
• 82,880 points
2,521 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
795 views
0 votes
0 answers

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL ...READ MORE

Aug 13, 2022 in Database by Kithuzzz
• 38,020 points
479 views
0 votes
0 answers

SQL Update from One Table to Another Based on a ID Match

Account and credit card numbers are stored ...READ MORE

Aug 22, 2022 in Database by Kithuzzz
• 38,020 points
1,179 views
0 votes
0 answers

MySQL Multiple Joins in one query?

I have the following query: SELECT dashboard_data.headline, ...READ MORE

Sep 4, 2022 in Database by narikkadan
• 63,680 points
471 views
0 votes
1 answer

SQL Inner-join with 3 tables?

Try this: SELECT s.studentname , ...READ MORE

answered Sep 15, 2022 in Database by narikkadan
• 63,680 points
1,101 views
0 votes
1 answer

How do I run an sql query in php?

Try this: <?php $conn = new mysqli('localhost', 'jaydeep_mor', 'jaydeep_mor', ...READ MORE

answered Sep 10, 2022 in Database by narikkadan
• 63,680 points
784 views
0 votes
1 answer

How do I obtain a Query Execution Plan in SQL Server?

There are several ways to get an ...READ MORE

answered Sep 20, 2022 in Database by narikkadan
• 63,680 points
1,240 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP