How to find duplicates in mongoDB with two conditions

38
November 21, 2020, at 10:10 PM

I want to find duplicates in the MongoDB atlas. I have seen examples for finding duplicates but they didn't take two conditions as I understand it. for some network issues, we discovered duplicates with a reference(refno) number in the database

so I want to find duplicate refno within these two conditions (currentDate and transactionTypeId)

 const transactionRecord = await this.tranModel.aggregate(
[{ $group: 
      { _id: "$refno",  count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }
])

With the above code I was able to get all the duplicates in the db, but I want to limit it to the condition

{ transactiontdate: currentDate, transactionTypeID: transactionTypesid });
Answer 1

As you only want to find duplicates, you can do the below.

db.coll.aggregate([
   {$group:{"_id":{
      dateCreated: "$currentDate", transactionTypeID: "$transactionTypeID"
   }}}
])

This will give you duplicate IDs.

Answer 2

I got it working

 const Record = await this.tranModel.aggregate([
                    {
                        $match: { transactiontdate: currentDate, transactionTypeID: transactionTypeID }
                    },
                    {
                        $group: { _id: "$refno", count: { $sum: 1 } }
                    }, {
                        $match: { count: { $gt: 1 } }
                    }
                ])
                if (Record != null) {
                    for (let i = 0; i < Record.length; i++) {
                        const element = Record[i]._id.toString()
                        tranArray.push(element)
                    }
                }
            }
READ ALSO
How to create a NPM command which executes a Go program?

How to create a NPM command which executes a Go program?

I have an NPM project (a React App) and want to execute a local go program by defining a command in the NPM packagejson

43
gulp.pipe() and src() not a function

gulp.pipe() and src() not a function

New to Gulp not sure what to do here I installed every dependency gulp, reactify and browserify

35
How to use promise.allSettled with typescript?

How to use promise.allSettled with typescript?

Typescript build is failing as it does not seem to like PromiseallSetttled even though I have set ts config comilerOptions with "lib": [ "ES2020

43
Is there any way to pass a React component to Push notification using service worker and node js?

Is there any way to pass a React component to Push notification using service worker and node js?

I am looking for a solution where I can pass a react component (let's take a modal component) to the push notification parameter using service worker and node jsMy service worker code for normal push notification code is-

43