In this tutorial, you'll learn how to join two collections in MongoDB using the aggregate method & lookup operator
—
Facebook: [ Ссылка ]
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn how to join two collections in the MongoDB database. Let's start by using the insert menu method to insert three documents in the property collection.
Note the property APN field is the unique identifier in this collection. Next, I'm going to use the insert many methods to insert two documents' taxes collection. Now use the find method to select all the fields from both the property and taxes collection to confirm that our inserts went through.
Finally to join these two collections together. I'm going to use the aggregate method with the lookup operator. Here, I'm running an aggregate method on the property collection and specifying the taxes collection in the form field.
I'm also setting the local field as property APN and the foreign field as APN. As is used to specify the name of the new array field. They'll be added to the input documents.
Now let's execute this query to see what the output looks like. There you have it. Make sure you like, subscribe, and turn on the notification bell.
Until next time.
Connect MySQL In Python: [ Ссылка ]
Run Selenium PyTests In Parallel: [ Ссылка ]
Find_elements() in Selenium: [ Ссылка ]
—
db.property.insertMany( [
{ "propertyApn" : 12345, "county" : "Valencia", "state" : "NM" },
{ "propertyApn" : 23334, "county" : "Elko", "state" : "NV" },
{ "propertyApn" : 98322, "county" : "Valencia", "state" : "NM" },
] )
db.taxes.insertMany( [
{ "apn" : 12345, "taxesDue": 423.77 },
{ "apn" : 23334, "taxesDue": 83.44 },
] )
db.property.find()
db.taxes.find()
db.property.aggregate( [
{
$lookup:
{
from: "taxes",
localField: "propertyApn",
foreignField: "apn",
as: "taxes_output"
}
}
] )
![](https://i.ytimg.com/vi/GYEPhjcJIFY/maxresdefault.jpg)