0

I have troubles at fltering subarrays with mongoose (MongoDB driver for NodeJS).

Here's an example which I'll use to show the problem:

[
    {
        employee: [list of IDs], // List of employees which are in this project
        entry: // Every employee can make entries
        [
            employee: ID //saving ID of employee
        ]
    },
]

What I want? I want to get whole objects (of employee and entry attributes) of those in the main array. That's how I do it:

mongoose.model('team').find({
    employee: "userid...", // Getting projects of an employee, but there's a missing part which I describe below.
}, function(err, teams) {
    res.send(teams);
})

That's working so far. But my problem is that I don't want to list the foreign entry objects in the nested array. So I want to filter this array. I tried this with aggregate and it worked BUT my problem is that I can't get the main parent when employee doesn't have any entries. So I want to get the parent object even when employee has nothing in the nested one.

I'd be very glad when you can help me out here.

Regards

yadbo
  • 407
  • 1
  • 5
  • 16
  • 3
    Your description is not clear. you can add comments in your code samples. – Heartbit Jan 01 '17 at 14:30
  • Possible dupe of https://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb – JohnnyHK Jan 01 '17 at 15:09
  • Thanks for your feedback @SaeidAlidadi, I added some more inforamtions. And it's similar to JohnnyHK, that was the aggregate solution, but it didn't help me to get the parent object when there are no entries in the nested array. – yadbo Jan 01 '17 at 21:24

0 Answers0