【发布时间】:2019-11-19 05:53:19
【问题描述】:
为了这个,我已经把头发拔了好几个星期了。
我有一个收藏(这是精简版):
const SubscriberSchema = new Schema({
publication: { type: Schema.Types.ObjectId, ref: "publicationcollection" },
buyer: { type: Schema.Types.ObjectId, ref: "buyercollection" },
postCode: { type: String },
modifiedBy: { type: String },
modified: { type: Date }
});
我还有一个包含 175 万个英国邮政编码的集合
const PostcodeSchema = new Schema({
postcode: { type: String }
});
我想要做的是返回订阅者集合中不存在于 Postcode 集合中的任何记录。
当我尝试使用 Mongoose 对订阅者集合中任何 >100 条记录进行非常简单的聚合时,我遇到超时或 >16MB 返回错误。
这是我迄今为止尝试过的:
router.get(
"/badpostcodes/:id",
passport.authenticate("jwt", { session: false }),
(req, res) => {
const errors = {};
Subscriber.aggregate([
{
$match: {
publication: mongoose.Types.ObjectId(req.params.id),
postCode: { "$ne": null, $exists: true }
}
},
{
$lookup: {
'from': 'postcodescollections',
'localField': 'postCode',
'foreignField': 'postcode',
'as': 'founditem'
}
},
// {
// $unwind: '$founditem'
// },
{
$match: {
'founditem': { $eq: [] }
}
}
], function (err, result) {
if (err) {
console.log(err);
} else {
if (result.length > 0) {
res.json(result);
} else {
res.json("0");
}
}
})
}
);
unwind 似乎没有做任何事情,但它被注释掉以表明我尝试使用它。
我也尝试过在查找上使用管道,但没有奏效,类似于以下内容(抱歉,我没有尝试原始代码,所以这仅来自内存):
$lookup: {
'from': 'postcodescollections',
'let': { 'postcode': "$postCode" },
'pipeline': [
{
'$match': {
'postcode': { $exists: false }
}
},
{
'$unwind': "$postCode"
}
],
'as': 'founditem'
}
提前谢谢,希望我能留住一些头发!
【问题讨论】:
标签: node.js mongodb express mongoose