Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

273
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda