Lookup (Joins)
Perform lookups (joins) on your collections.
Before we get started, please review the Lookup Pipeline documentation.
Method Signature
public lookup(options: LookupPipelineOptions): this;
Where LookupOptions is:
export type LookupPipelineOptions = {
from: string;
localField?: string;
foreignField?: string;
as?: string;
single?: boolean;
pipeline?: (Pipeline | GenericObject)[];
let?: GenericObject;
};
Example
Let's see a basic example, let's see we need to load posts with the updated data of the authors from users collection.
const posts = await new Aggregate("posts")
.lookup({
from: "users",
localField: "author.id",
foreignField: "id",
as: "author",
single: true,
})
.get();
By adding single option, it will return the first document in the array.
Lookup with multiple documents
Let's load posts with their comments.
const posts = await new Aggregate("posts")
.lookup({
from: "comments",
localField: "id",
foreignField: "postId",
as: "comments",
})
.get();
This will load with each post all the comments that belong to it.
Lookup with pipeline
Let's load posts with their comments, but we want to load only the comments that have more than 10 likes.
const posts = await new Aggregate("posts")
.lookup({
from: "comments",
localField: "id",
foreignField: "postId",
as: "comments",
pipeline: [
{
$match: {
likes: {
$gt: 10,
},
},
},
],
})
.get();
Calling MongoDB Aggregate stages
Most of MongoDB Aggregate stages are exported separately, so we can use them directly in the pipelines.
Let's load posts with their comments, but we want to load only the comments that have more than 10 likes.
import { wherePipeline, parsePipelines } from "@mongez/monpulse";
const posts = await new Aggregate("posts")
.lookup({
from: "comments",
localField: "id",
foreignField: "postId",
as: "comments",
pipeline: parsePipelines([wherePipeline("likes", ">", 10)]),
})
.get();
Available stages to use
Here are the available stages to use with parsePipelines function:
wherePipeline: Receives same arguments aswheremethod.limitPipeline: Receives same arguments aslimitmethod.skipPipeline: Receives same arguments asskipmethod.sortPipeline: Receives same arguments assortmethod.sortByPipeline: Receives same arguments assortBymethod.sortRandomPipeline: Receives same arguments asrandommethod.selectPipeline: Receives same arguments asselectmethod.deselectPipeline: Receives same arguments asdeselectmethod.groupByPipeline: Receives same arguments asgroupBymethod.lookupPipeline: Receives same arguments aslookupmethod.orWherePipeline: Receives same arguments asorWheremethod.unwindPipeline: Receives same arguments asunwindmethod.
Lookup with let
Let's load posts with their comments, but we want to load only the comments that have more than 10 likes.
const posts = await new Aggregate("posts")
.lookup({
from: "comments",
localField: "id",
foreignField: "postId",
as: "comments",
let: {
postId: "$id",
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$postId", "$$postId"],
},
{
$gt: ["$likes", 10],
},
],
},
},
},
],
})
.get();
This is just the native MongoDB syntax, you can use it directly in the
pipelineoption.