MongoDB/Mongoose 聚合数据重复,显示自动创建的 ID 时出现问题

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

我有一个客户数据库,我想聚合我的数据并在前端显示所有重复项,我正在检查我对 Insomnia 的获取请求,我正确地取回了所有数据,当我想显示ID,如何实现?

这是我的客户架构:

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

这是我的聚合控制器:

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};

当我尝试通过 Insomnia 发送带有注释部分代码的获取请求时,这是我的结果:

[
    {
        "_id": {
            "title": "Jr",
            "salutation": "Mrs",
            "firstName": "Korey",
            "lastName": "Signoret",
            "companyName": "Oyoloo",
            "business": true,
            "street": "51 Annamark Parkway",
            "city": "Bryansk",
            "postcode": "241904",
            "email": "ksignoret19@youku.com",
            "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": "ksignoret19@youku.com",
        "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": "jgooly1a@stanford.edu",
            "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": "jgooly1a@stanford.edu",
        "phone": "864-794-2909"
    }
]

当我试图取消注释这部分代码时,这就是我的结果

[]

获取重复项正常工作,我不会 post 所有重复项(重复项太多)但上面是响应数据,目前其他数据 return 正确但不正确身份证

是因为ID是Mongo自动生成的吗?必须由我在 Schema 中手动设置?如何修复我的代码?我是新手,请耐心等待,如果有人需要更多信息,请告诉我:)

我已经显示了我的 ID,这就是我使用 $addToSet 运算符实现的方式。

这里 mongo 文档: https://www.mongodb.com/docs/manual/reference/operator/update/addToSet/

这就是我的控制器的样子

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};