0

I am new to MongoDb and I want to remove duplicate rows by making UID unique based on startDate and endDate range. For example i have the following documents represents employee records.Here the employee can exists in different date range(eg: If "UID":11 inserted within "startDate":"2019-02-19" & "endDate":"2019-05-19". but the same emloyee with UID:11 should not be inserted within same date range and can be inserted within different date range(eg: UID:11 can be inserted within "startDate" :"2020-02-19","endDate":"2020-06-19").So how can I prevent the rows from duplicating by using this condition. Hope you understand my query.Thanks in advance.

{  
        "_id" : ObjectId("5ef416bb2791f640a477f852"),  
        "UID" : 11,  
        "EmployeeName" : "Manu",  
        "startDate" : ISODate("2019-02-19T00:00:00Z"),  
        "endDate" : ISODate("2019-05-19T00:00:00Z")  
}

{  
        "_id" : ObjectId("5ef417422791f640a477f853"),  
        "UID" : 11,  
        "EmployeeName" : "Manu",  
        "startDate" : ISODate("2019-02-19T00:00:00Z"),  
        "endDate" : ISODate("2019-05-19T00:00:00Z")  
}

{  
        "_id" : ObjectId("5ef4176a2791f640a477f854"),  
        "UID" : 11,  
        "EmployeeName" : "Manu",  
        "startDate" : ISODate("2020-02-19T00:00:00Z"),  
        "endDate" : ISODate("2020-06-19T00:00:00Z")  
}

I have tried this below query with my knowledge but didn't work...

db.testing.distinct("UID","startDate":{"$gte":ISODate("2019-02-19")},"endDate":{"$lte":ISODate("2019-05-19")})
5
  • Do you already have the data inserted or do you want to create a script that insert this data and prevent duplicate? Not sure from where you are starting. Jun 25, 2020 at 5:56
  • yes I already have the data and posting to the db.
    – Manmadh
    Jun 25, 2020 at 6:08
  • I suggest you to provide a more clear input and most important output example. For example, startDate and endDate in record #1 and #2 contains the same data and this is easy to be solved: can we assume this is the pattern you are going to solve? Jun 25, 2020 at 6:13
  • when we try to insert a record by record, the record #1 will get inserted and when comes to record #2 it won't get inserted as we are trying to insert same employee(UID:11)duplicate row within the same date range. And when we try to insert the record #3, it will successfully get inserted bcoz the employee comes under different date range.
    – Manmadh
    Jun 25, 2020 at 6:27
  • then the output will be like: { "_id" : ObjectId("5ef416bb2791f640a477f852"), "UID" : 11, "EmployeeName" : "Manu", "startDate" : ISODate("2019-02-19T00:00:00Z"), "endDate" : ISODate("2019-05-19T00:00:00Z") } { "_id" : ObjectId("5ef4176a2791f640a477f854"), "UID" : 11, "EmployeeName" : "Manu", "startDate" : ISODate("2020-02-19T00:00:00Z"), "endDate" : ISODate("2020-06-19T00:00:00Z") }
    – Manmadh
    Jun 25, 2020 at 6:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.