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

275
Views
How can I combine two collection in mongoDB and also fulfill the following conditions?

Note: Each collection contains 96.5k documents and each collection have these fields --

{
  "name": "variable1",
  "startTime": "variable2",
  "endTime": "variable3",
  "classes": "variable4",
  "section": "variable"
}

I have 2 collections. I have to compare these 2 collection and have to find out whether some specific fields( here I want name, startTime, endTime) of the documents are same in both the collection.

My approach was to join these 2 collection and then use $lookup .. I also tried the following query but it didn't work. Please help me.

col1.aggregate([
    {
        "$unionWith": {"col1": "col2"}
    },
    {
        "$group":
            {
                "_id":
                    {
                        "Name": "$Name",
                        "startTime": "$startTime",
                        "endTime": "$endTime"
                    },
                "count": {"$sum": 1},
                "doc": {"$first": "$$ROOT"}
            }
    },
    {
        "$match": {"$expr": {"$gt": ["$count", 1]}}
    },
    {
        "$replaceRoot": {"newRoot": "$doc"}
    },
    {
        "$out": "newCollectionWithDuplicates"
    }
])
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're approach is fine you just have a minor syntax error in your $unionWith, it's suppose to be like so:

{
  "$unionWith": {
    coll: "col2",
    pipeline: []
  }
}

Mongo Playground

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!