Answer the question
In order to leave comments, you need to log in
How to use $all in mongoDB aggregation?
There are 2 tables, and I take documents from them through aggregation.
I take from the second table like this:
let employees = await models.employee.aggregate([
{
$match: {
login: login.toLowerCase(),
},
},
{
$lookup: {
from: "positiontypes",
let: {
position: "$position",
function: "$function",
bloc: "$bloc",
specialProperties: "$specialProperties",
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$$position", "$position"] },
{ $eq: ["$$function", "$function"] },
{ $eq: ["$$bloc", "$bloc"] },
{
$eq: [
"$$specialProperties",
"$specialProperties",
],
},
],
},
},
},
],
as: "positiontype",
},
},
]);
Answer the question
In order to leave comments, you need to log in
Figured out this query:
let employees = await models.employee.aggregate([
{
$match: {
login: login.toLowerCase(),
},
},
{
$lookup: {
from: "positiontypes",
let: {
position: "$position",
function: "$function",
bloc: "$bloc",
specialProperties: "$specialProperties",
},
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$$position", "$position"] },
{ $eq: ["$$function", "$function"] },
{ $eq: ["$$bloc", "$bloc"] },
{
$allElementsTrue: {
$map: {
input: "$$specialProperties",
as: "elem",
in: {
$and: {
$in: [
"$$elem",
"$specialProperties",
],
},
},
},
},
},
],
},
},
},
],
as: "positiontype",
},
}, ]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question