My program generate 2 collection everyday in my database. Each day I have to compare these 2 collection and have to find out whether some specific elements of the documents are same in both the collection. If they are same I have to store the document somewhere.
Note: Each collection have 94.6k documents. Each document contains these columns or elements – _id(which is different all the time), studentName, timeOfEntry, timeOfExit, section, class.
*I also have one more collection named as names which. contains name of all students(which are unique) *
I have to find out which document in the 2 collection have same value for studentName, timeOfEntry and timeOfExit. How can I achieve this?
Example:
Like in collection1 -- a document contain the value of student:"Isha" ,entryTime:5pm and exitTime: 7pm.
collection 2 also contain a document with these values.
My approach: ** I was trying to create a function in springboot java where -- function(){ compare the 2 collection }**
CodePudding user response:
Query
- union the 2 collections
- group by the 3 fields, and count the occurrences
- keep only the duplicates (count>1)
- fix the structure of the document
$outto write to the new collection
coll1.aggregate(
[{"$unionWith":{"coll":"coll2"}},
{"$group":
{"_id":
{"studentName":"$studentName",
"timeOfEntry":"$timeOfEntry",
"timeOfExit":"$timeOfExit"},
"count":{"$sum":1},
"doc":{"$first":"$$ROOT"}}},
{"$match":{"$expr":{"$gt":["$count", 1]}}},
{"$replaceRoot":{"newRoot":"$doc"}}
{"$out": "newCollectionWithDuplicates"}])
