我必须收集我订阅的客户,历史记录集合中包含所有客户(已订阅和未订阅)。 要获得唯一客户,应使用3个字段(id,idType和countryCode)
customers example doc
{
id:'123456789',
idType: 1,
countryCode: 1234,
... more info
}
history example doc
{
id:'123456789',
idType: 1,
countryCode: 1234,
expired: false,
...more info
}
我应该获取客户集合中不存在的所有客户历史记录,并将过期字段更新为true
我的汇总功能,用于获取所有未订阅的客户
db.history.aggregate([
$group: { // use $group for distingt
_id: {
id: "$id",
idType: "$idType",
countryCode: "$countryCode"
}
},
{
$lookup: {
from: "customers",
let: { historyId: "$_id.id", historyIdType: "$idType", historyCountryCode: "$countryCode"},
pipeline: [
{
$match: {
$expr: {
$and: [
{...}, //if id equal
{...}, //if idType equal
{...} //if countryCode equal
]
}
}
}
],
as: "customer"
}
},
{
$project: {
customerObjId: { $arrayElemtAt: ["$customer", 0]}
}
},
{
$project: {
customerId: "$customerObjId._id"
}
},
{
$match: {
customerId: { $exists: false}
}
}
]);
结果:
{
_id: {
id: "123343456578",
idType: "1",
countryCode: "1234"
}
}
....more docs
现在,我需要更新所有未订阅客户的所有历史记录。