MongoDB汇总重复

发布于 2025-01-24 14:37:11 字数 3364 浏览 1 评论 0原文

是否可以使用Mongo聚合框架在不同对象键中查找对象值重复的选项?更明确的是,我显示了我的查询以及一些输入和预期的输出:

我的聚合查询:

const Customer = require('../models/Customer');

const getDataDuplicates = (req, res) => {
  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}
      }
    },
    {
      $sort: {"count": -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);
    }).catch((e) => {
    res.send(e)
  })
}
module.exports = {getDataDuplicates};

现在,当所有对象数据都相同时,我的查询只有一种情况。但是如果:

  1. 两个客户将具有相同的名称,LastName,CompanyName和Email,但其他人将具有相同的电子邮件,街道和电话等。

在这里汇总之前的示例数据输入:

    {
        "_id": "6259715edaa388eb639b2ce5",
        "title": "Prof",
        "salutation": "Herr",
        "firstName": "Bartlomiej",
        "lastName": "Slapinski",
        "companyName": "ITSD",
        "business": false,
        "street": "sadasd",
        "city": "Krefeld",
        "postcode": "47805",
        "email": "[email protected]",
        "phone": "2132142143214214",
        "__v": 0
    },
    {
        "_id": "246e9e89c29750f52974566b",
        "title": "Dr",
        "salutation": "Firma",
        "firstName": "Bartlomiej", // duplicate
        "lastName": "Slapinski", // duplicate
        "companyName": "ITSD", // duplicate
        "business": true,
        "street": "Gigstr. 24", 
        "city": "Egal", 
        "postcode": "47225", 
        "email": "[email protected]", //duplicate
        "phone": "2132142143",
        "__v": 0
    },

上面的输入显示名称,姓氏,公司名称和电子邮件是相同的,因此查询结果应该看起来像这样:

    {
        "_id": [
            "6259715edaa388eb639b2ce5",
            "246e9e89c29750f52974566b"
        ],
        "firstName": "Bartlomiej",
        "lastName": "Slapinski",
        "companyName": "ITSD",
        "email": "[email protected]",
    },

因此逻辑应该看起来像这样:

Loop through $group and check if $email is same $or firstName $or and so on and so on ...

如果有什么尚不清楚的话,请提出问题,让我知道我会发布更多信息,谢谢!

Is there an option to find Object values duplicates in different object keys with Mongo aggregate framework ? To be more clear, I show my query and some input and expected output:

My aggregation query:

const Customer = require('../models/Customer');

const getDataDuplicates = (req, res) => {
  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}
      }
    },
    {
      $sort: {"count": -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);
    }).catch((e) => {
    res.send(e)
  })
}
module.exports = {getDataDuplicates};

For now my query handles only one situation when all object data is the same. But what if :

  1. Two Customers will have for example the same firstName, lastName, companyName and email, but other one will have for example same email, street and phone etc.

Here example data input before aggregation :

    {
        "_id": "6259715edaa388eb639b2ce5",
        "title": "Prof",
        "salutation": "Herr",
        "firstName": "Bartlomiej",
        "lastName": "Slapinski",
        "companyName": "ITSD",
        "business": false,
        "street": "sadasd",
        "city": "Krefeld",
        "postcode": "47805",
        "email": "[email protected]",
        "phone": "2132142143214214",
        "__v": 0
    },
    {
        "_id": "246e9e89c29750f52974566b",
        "title": "Dr",
        "salutation": "Firma",
        "firstName": "Bartlomiej", // duplicate
        "lastName": "Slapinski", // duplicate
        "companyName": "ITSD", // duplicate
        "business": true,
        "street": "Gigstr. 24", 
        "city": "Egal", 
        "postcode": "47225", 
        "email": "[email protected]", //duplicate
        "phone": "2132142143",
        "__v": 0
    },

Above input shows that name, lastname, company Name and email are the same so the query results should look like this:

    {
        "_id": [
            "6259715edaa388eb639b2ce5",
            "246e9e89c29750f52974566b"
        ],
        "firstName": "Bartlomiej",
        "lastName": "Slapinski",
        "companyName": "ITSD",
        "email": "[email protected]",
    },

So the logic should look something like these:

Loop through $group and check if $email is same $or firstName $or and so on and so on ...

if anything is unclear please ask questions and let me know i will post more info, Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文