0

I would like to aggregate $match on the data inside the array object:

{ example: [{target: "1"}] }

I have tried $match, $elemMatch, $arrayEleAt but I dont know how to write the correct syntax.

{ $lookup: { from: "example", localField: "user_id", foreignField: "user_id", as: "example" } },

    {
        $match: {
            "start_date": { $gte: new Date(startDate) },
            "end_date": { $lte: new Date(endDate) },
            "type": Type,
            "target": "1"

        },
    },

    { $sort: { startDate: 1 } },

1 Answer 1

1

Just write it like this:

{
    $match: {
        "start_date": { $gte: new Date(startDate) },
        "end_date": { $lte: new Date(endDate) },
        "type": Type,
        "example.target": "1"

    },
},
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I found out thank you. But I added $unwind able to match this way because lookup inserts object into an Array. { $lookup: { from: "example", localField: "user_id", foreignField: "user_id", as: "example" } }, { $unwind: "$example" }, { $match: { "start_date": { $gte: new Date(startDate) }, "end_date": { $lte: new Date(endDate) }, "type": Type, "example.target": "1" }, },
so it works for you?, if not edit your question to match your full query and ill try to help.
Yes, thank you Tom, it does. I have had to use $unwind to remove the array. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.