Home > Software design >  How can I combine two collection in mongoDB and also fulfill the following conditions?
How can I combine two collection in mongoDB and also fulfill the following conditions?

Time:01-06

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"
    }
])

CodePudding user response:

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

  •  Tags:  
  • Related