Home > Net >  MongoDB/Mongoose Aggregate Data Duplicates, problem with displaying automatically created ID
MongoDB/Mongoose Aggregate Data Duplicates, problem with displaying automatically created ID

Time:04-24

I have a Customer DB and I want to aggregate through my data and display all the duplicates on frontend, I'm checking my get request with Insomnia and I get all the data back properly, problem occurs when I'm trying to display ID, how to achieve it ?

Here is my Customer Schema :

const CustomerSchema = new mongoose.Schema({
  title: String,
  salutation: String,
  firstName: String,
  lastName: String,
  companyName: String,
  business: Boolean,
  street: String,
  city: String,
  postcode: String,
  email: String,
  phone: String
})

And here is my aggregate controller:

const Customer = require('../models/Customer');
let mongodb = require('mongodb');
const mongoose = require('mongoose');

const getDataDuplicates = (req, res) => {
  // aggregate Data - find duplicates logic

// TODO DISPLAY ID for duplicate
  Customer.aggregate([
    {
      $group: {
        _id: {
          // _id: "$_id",
          title: "$title",
          salutation: "$salutation",
          firstName: "$firstName",
          lastName: "$lastName",
          companyName: "$companyName",
          business: "$business",
          street: "$street",
          city: "$city",
          postcode: "$postcode",
          email: "$email",
          phone: "$phone",
        },
        count: {$sum: 1}
      }
    },
    {
      $match: {
        count: {$gt: 1}
      }
     },
    {
      $project: {
        // "_id" : {
        //   $toString: "$_id._id"
        // },
        "title": "$_id.title",
        "salutation": "$_id.salutation",
        "firstName": "$_id.firstName",
        "lastName": "$_id.lastName",
        "companyName": "$_id.companyName",
        "business": "$_id.business",
        "street": "$_id.street",
        "city": "$_id.city",
        "postcode": "$_id.postcode",
        "email": "$_id.email",
        "phone": "$_id.phone"
      },
    },

  ]).then((data) => {
    res.send(data);
  }).catch((e) => {
    res.send(e)
  })

}

module.exports = {getDataDuplicates};

When I'm trying to send get request via Insomnia with commented part of code this is my result:

[
    {
        "_id": {
            "title": "Jr",
            "salutation": "Mrs",
            "firstName": "Korey",
            "lastName": "Signoret",
            "companyName": "Oyoloo",
            "business": true,
            "street": "51 Annamark Parkway",
            "city": "Bryansk",
            "postcode": "241904",
            "email": "[email protected]",
            "phone": "571-657-2625"
        },
        "title": "Jr",
        "salutation": "Mrs",
        "firstName": "Korey",
        "lastName": "Signoret",
        "companyName": "Oyoloo",
        "business": true,
        "street": "51 Annamark Parkway",
        "city": "Bryansk",
        "postcode": "241904",
        "email": "[email protected]",
        "phone": "571-657-2625"
    },
    {
        "_id": {
            "title": "Jr",
            "salutation": "Honorable",
            "firstName": "Jocelin",
            "lastName": "Gooly",
            "companyName": "Oyoyo",
            "business": false,
            "street": "878 Myrtle Parkway",
            "city": "Casal",
            "postcode": "4600-757",
            "email": "[email protected]",
            "phone": "864-794-2909"
        },
        "title": "Jr",
        "salutation": "Honorable",
        "firstName": "Jocelin",
        "lastName": "Gooly",
        "companyName": "Oyoyo",
        "business": false,
        "street": "878 Myrtle Parkway",
        "city": "Casal",
        "postcode": "4600-757",
        "email": "[email protected]",
        "phone": "864-794-2909"
    }
]

When I'm trying to uncomment this part of code this is my result

[]

Getting duplicates work correctly, I won't post all the duplicates (there is too much of them) but above is the response data, for now other data is return correctly but not the ID

Is that because ID is automatically created by Mongo ? Must be manually set by me in Schema ? How to fix my code? Im a newbie please be patient, if some1 need more info please just let me know :)

CodePudding user response:

I've got my id's displayed, this is how I made it with $addToSet operator.

Here mongo docs: https://www.mongodb.com/docs/manual/reference/operator/update/addToSet/

and this is how my controller looks like

const Customer = require('../models/Customer');
let mongodb = require('mongodb');
const mongoose = require('mongoose');


const getDataDuplicates = (req, res) => {
  // aggregate Data - find duplicates logic

// TODO DISPLAY ID for duplicate
  Customer.aggregate([
    {
      $group: {

        _id: {
          title: "$title",
          salutation: "$salutation",
          firstName: "$firstName",
          lastName: "$lastName",
          companyName: "$companyName",
          business: "$business",
          street: "$street",
          city: "$city",
          postcode: "$postcode",
          email: "$email",
          phone: "$phone",
        },
        uniqueIds: {$addToSet: "$_id"},

        count: {$sum: 1}
      }
    },
    {
      $match: {
        count: {$gt: 1}
      }
     },
    {
      $project: {
        "_id" : "$uniqueIds",
        "title": "$_id.title",
        "salutation": "$_id.salutation",
        "firstName": "$_id.firstName",
        "lastName": "$_id.lastName",
        "companyName": "$_id.companyName",
        "business": "$_id.business",
        "street": "$_id.street",
        "city": "$_id.city",
        "postcode": "$_id.postcode",
        "email": "$_id.email",
        "phone": "$_id.phone"
      },
    },

  ]).then((data) => {
    res.send(data);
    console.log(data)
  }).catch((e) => {
    res.send(e)
  })

}

module.exports = {getDataDuplicates};

  • Related