$in
を使用できます
配列が空でなく、 $nin
の場合
配列が空の場合、この方法では一致フィールドは考慮されません($nin : []
):
function buildMatch(arr) {
var matcher = {};
if (arr.length == 0)
matcher["$nin"] = arr;
else
matcher["$in"] = arr;
return matcher;
}
var grades = ["09", "10", "11", "12"];
var areas = [ "English 2" ];
var gradeMatch = buildMatch(grades);
var areaMatch = buildMatch(areas);
db.students.aggregate([{
$match: {
"school._id": "7011",
"studentGradeLevels": gradeMatch,
"contentArea": areaMatch
}
}])