Aggregation pipeline to achieve inner join:
LEFTT Collection:
Comments Collection:
Now we're going to implement inner join with aggregation.
Db.comments.aggregate ([{$lookup: {from:"LEFTT", Localfield:"timestamp", Foreignfield:" Timestamp "" Timestampcol"" $timestampCol "},{$project: {" Ratingeq ": {" $eq ": [" $rating "," $ Timestampcol.rating "]}," timestamp ": 1," rating ": 1, " Timestampcol.timestamp ": 1}},{$match: {" Ratingeq " :true}}])
Isn't it amazing?
Step parsing:
- $lookup: Implements timestamp equivalent connection, equal LEFTT result is placed in Timestampcol collection
- $unwind: Implemented to flatten the LEFTT array, there are multiple
- $project: Implementation of the second equivalent connection rating, and placed in the Ratingeq field
- $match: Take only Ratingeq is true, indicating that the second rating field is equal
MongoDB Learning Note Aggregation Pipeline actual combat implementation inner JOIN