[英]How to move document from one collections to another? Mongoose, MongoDB
[英]Get result based on property of another document from one document in MongoDB
我有两个文件——
const taskSchema = new mongoose.Schema({
leadId: { type: String, default: '' },
customer_name: { type: String, default: '' }
})
const leadSchema = new mongoose.Schema({
Id: { type: String, default: "" },
associate_status: { type: Boolean, default: false },
})
我想对任务运行查询,以便获得关联状态为真的所有潜在客户。
任务架构中的LeadId
与潜在客户架构中的Id
相同。
我试过这个 -
const report = await taskModel.aggregate([
{ $lookup: {
from: 'leads',
let: { leadId: '$leadId' },
pipeline: [
{
$match: {
$expr: {
$eq: ['$Id', '$$leadId'],
},
},
},
],
as: 'leaditems',
},
}
{ $unwind: '$leaditems' },
{ $match: { leaditems.associate_status: true}}
])
但是由于我有大量文档(超过 200k 潜在客户和 100k 任务,并且这些数字将继续增长),查询有时会在 9 到 10 秒后运行,有时根本不运行。 我正在寻找一种更优化的方法或任何其他可以使这项工作发挥作用的实现。
更新:
我也试过这个,但没有改善 -
const report = await taskModel.aggregate([
{ $lookup: {
from: 'leads',
let: { leadId: '$leadId' },
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ['$Id', '$$leadId'],
},
{
$eq: ['$associate_status', true],
},
],
},
},
},
],
as: 'leaditems',
},
}
])
将$match
条件放在$lookup
pipeline
,甚至尝试将其放在$lookup
之前。
在两个关联的 id 上设置index
。
也许你的服务有一个 10 秒的数据库连接限制,试着让它更长。
要在查找管道内使用$exp
的索引,您需要 MongoDB 5。MongoDB 4.4.9 不会使用索引,查询会很慢。
$lookup
$unwind
并且您更喜欢数组中的数据,则可以使用$filter
const report = await taskModel.aggregate([
{ $lookup: {
from: 'leads',
localField: "leadId",
foreignField: "Id",
as : "leaditems"
},
{ $unwind: '$leaditems' },
{ $match: { leaditems.associate_status: true}}
])
如果可能的话,在你完成这些并发送一些反馈后对其进行基准测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.