MongoDB汇总重复
是否可以使用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};
现在,当所有对象数据都相同时,我的查询只有一种情况。但是如果:
- 两个客户将具有相同的名称,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 :
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论