我有一個客戶資料庫,我想匯總我的資料并在前端顯示所有重復項,我正在使用 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": "[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"
}
]
當我試圖取消注釋這部分代碼時,這是我的結果
[]
獲取重復項正常作業,我不會發布所有重復項(它們太多)但上面是回應資料,現在其他資料正確回傳但不是 ID
那是因為 ID 是由 Mongo 自動創建的嗎?必須由我在 Schema 中手動設定嗎?如何修復我的代碼?我是新手,請耐心等待,如果有人需要更多資訊,請告訴我:)
uj5u.com熱心網友回復:
我已經顯示了我的 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};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/465393.html
上一篇:從另一個集合中的資料提高搜索分數
下一篇:如何“加入”或填充陣列
