I have several solutions, which in term may hold several projects. I modeled this relationship by embedding the projects in the solution document, e.g.
[{
_id: "1",
solutionTitle: "Some Test Solution",
projects: [
{
_id: "12",
type: "Java",
title: "Test Project"
},
{
_id: "13",
type: "Misc",
title: "Test Project"
}
]
},
{
_id: "2",
solutionTitle: "A Different Solution",
projects: [
{
_id: "21",
type: "Java",
title: "Another Java Project"
}
]
}]
Now I want to select all projects of a special type, e.g. Java. I tried the following query with aggregation:
db.Solutions.aggregate (
{ "$unwind": "$projects" },
{ "$match": {"projects.type": "Java" } },
{ "$project": {"projects" : 1, "_id": 0, "solutionTitle": 0 } }
)
This works fine but the result doesn't look like what I expected. I get
{
projects: {
_id: "12",
type: "Java",
title: "Test Project"
},
projects: {
_id: "21",
type: "Java",
title: "Another Java Project"
}
}
How can I get the result to be a list of projects, e.g.
[
{ _id: "12", type: "Java", title: "Test Project" }
...
]
I checked this SO question and this one, but they do not really cover what I need. Any help is greatly appreciated.