mongoseを使用して連表クエリーとグループ化($group)、フィルタ($match)、指定フィールド($project)、一括クエリー($in)
nodejsエクスポートxlsx、サポートスタイル、お読みください!
const arrAttribute = await models.institutionAttributes.aggregate(
[
{
$lookup: , mysql left join
{
from: "exhibition_institutions",//
localField: "_id",//
foreignField: "attribute",//
as: "institutionsData"//
}
},
{
$match:// , mysql where
{
_id: mongoose.Types.ObjectId(team),// , id,
}
},
{
$project://
{
_id: 0,// _id, 0,
name: 1,// name
"institutionsData._id": 1,//
}
},
]
);
// ,
let arrAttributeSort = [];
for (let i = 0; i < arrAttribute.length; i++) {
for (let j = 0; j < arrAttribute[i].institutionsData.length; j++) {
arrAttributeSort.push(arrAttribute[i].institutionsData[j]._id);
}
}
const signData = await models.exhibitionItem.aggregate(
[
{
$lookup:
{
from: "signups",
localField: "_id",
foreignField: "exhibition_item",
as: "signInstitutionsData"
}
},
{
$match:
{
"signInstitutionsData": { $ne: [] },//$ne:[]
from: { $gte: parseInt(from) },,//
to: { $lte: parseInt(to) },//
"signInstitutionsData.institutions.institution": { $in: arrAttributeSort }//$in ,[1,2,3,4,5]
}
},
{
$project:
{
_id: 0,
"signInstitutionsData.unit_name": 1,
"signInstitutionsData.institutions": 1
}
}
]
);