我有一个汇总呼叫,如下所示。我相信发生角色查找时会造成大量延迟,因为有申请人,受雇人员和拒绝人员来从资源表中查找。资源集合中有10,000多个条目,因此查询大约需要6秒钟。我在做什么,这在这里是非常错误的?我看不到如何使用索引,因为所有查找都是通过默认已被索引的_id完成的。
应用程序,雇用字段和拒绝字段只是对象ID的数组,例如
applicants: [
ObjectId('asldkajsdlkj'),
ObjectId('asldkjaoksdjak')
]
任何帮助将不胜感激。在M0实例上花费了6秒,而在M10实例上并没有更快。
return db.collection('projects').aggregate([
{
$match: {
agents: ObjectId('SOMETHING')
}
},
{
$lookup: {
from: "agents",
localField: "agents",
foreignField: "_id",
as: "agents"
}
},
{
$lookup: {
from: "agencies",
localField: "agency",
foreignField: "_id",
as: "agency"
}
},
{
$lookup: {
from: "roles",
let: { "roles": "$roles" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },
{
$lookup: {
from:"resources",
let: { "applicants": "$applicants" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$applicants" ] } } }
],
as: "applicants"
}
},
{
$lookup: {
from: "resources",
let: { "hired": "$hired" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$hired" ] } } }
],
as: "hired"
}
},
{
$lookup: {
from: "resources",
let: { "rejected": "$rejected" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$rejected" ] } } }
],
as: "rejected"
}
},
{
$lookup: {
from: "agents",
localField: "hiring_agent",
foreignField: "_id",
as: "hiring_agent"
}
}
],
as: "roles"
}
}
], {
allowDiskUse: true
})
答案 0 :(得分:1)
MongoDB旨在存储“煮熟”的数据。这意味着,在project
集合中,您需要存储冗余信息以转换为所需的结果。
在您的情况下,由于to this error,$lookup
集合的内部let - pipeline
与role
的性能下降。
尝试更改:
db.collection('projects').aggregate([
{
$match: {
agents: ObjectId('SOMETHING')
}
},
{
$lookup: {
from: "agents",
localField: "agents",
foreignField: "_id",
as: "agents"
}
},
{
$lookup: {
from: "agencies",
localField: "agency",
foreignField: "_id",
as: "agency"
}
},
{
$lookup: {
from: "roles",
let: { "roles": "$roles" },
pipeline: [
{ $match: { $expr: { $in: [ "$_id", "$$roles" ] } } },
{
$lookup: {
from:"resources",
localField: "applicants",
foreignField: "_id",
as: "applicants"
}
},
{
$lookup: {
from: "resources",
localField: "hired",
foreignField: "_id",
as: "hired"
}
},
{
$lookup: {
from: "resources",
localField: "rejected",
foreignField: "_id",
as: "rejected"
}
},
{
$lookup: {
from: "agents",
localField: "hiring_agent",
foreignField: "_id",
as: "hiring_agent"
}
}
],
as: "roles"
}
}
], {
allowDiskUse: true
})