Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
How can I get only the first matching record in a mongoDb join using lookup?

Imagine two mongo collections.

FirstCollection is formatted like this:

{
    "JoinField": "this is my data"
}

SecondCollection is formatted like this (the real data is more complex and needs flattening):

{
    "FirstField" : "food",
    "SecondField": "bar",
    "Properties" : {
        "JoinField": "this is my data"
    }
}

SecondCollection contains duplicates and I'm trying to join the two tables and flatten the data into one projection.

This query will broadly do what I want:

db.getCollection('FirstCollection').aggregate([ 
{
    "$lookup": {
        from: 'SecondCollection',
        localField: 'JoinField',
        foreignField: 'Properties.JoinField',
        as: 'secondCollection'
    }
},
{ 
    "$unwind": "$secondCollection"
},
{
    "$project": {
        "JoinField" : 1,
        firstField : "$secondCollection.FirstField",
        secondField : "$secondCollection.SecondField",
    }
}
])

The only problem is that this contains duplicates in itself because it repeats the data for each duplicate in SecondCollection that matches a line in FirstCollection.

How can I only get the first record (or any single copy of the record) from SecondCollection?

EDIT: Apologies for lack of clarity. What I will currently get from that query, presuming a duplicate in SecondCollection, is this:

{
    "JoinField": "this is my data".
    "FirstField" : "food",
    "SecondField": "bar"
}

{
    "JoinField": "this is my data".
    "FirstField" : "food",
    "SecondField": "bar"
}

What I want is this:

{
    "JoinField": "this is my data".
    "FirstField" : "food",
    "SecondField": "bar"
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try lookup with aggregation pipeline from MongoDB v3.6,

  • let to pass local field to second collection pipeline,
  • pipeline to match field and, set limit to return single document
  {
    "$lookup": {
      from: "SecondCollection",
      let: { "jfield": "$JoinField" },
      pipeline: [
        { $match: { $expr: { $eq: ["$Properties.JoinField", "$$jfield"] } } },
        { $limit: 1 }
      ],
      as: "secondCollection"
    }
  },

Playground


Second option from MongoDB v3.2, put a $set stage after $lookup and before $unwind,

  • $arrayElemAt return element from specific position
  {
    $set: {
      secondCollection: { $arrayElemAt: ["$secondCollection", 0] }
    }
  }

Playground

over 4 years ago · Santiago Trujillo Report

0

If I understand the question correctly, you could try this

db.b.aggregate([ //Used second collection as the source
  {
    "$group": { //Removing duplicates in the join field
      "_id": "$properties.joinField",
      FirstField: {
        "$addToSet": "$FirstField"
      },
      SecondField: {
        "$addToSet": "$SecondField"
      },
      "JoinField": {
        $first: "$Properties.JoinField"
      }
    }
  },
  {
    "$lookup": {
      from: "a",
      localField: "JoinField",
      foreignField: "JoinField",
      as: "FirstCollection"
    }
  },
  {
    "$unwind": "$FirstCollection"
  },
  {
    "$project": {
      "JoinField": 1,
      firstField: "$FirstField",
      secondField: "$SecondField",
      
    }
  }
])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!