I have multiple solutions
, each of which may contain various projects
. To represent this relationship, I opted for embedding
the projects within the solution document. For example:
[{
_id: "1",
solutionTitle: "Some Sample Solution",
projects: [
{
_id: "12",
type: "Java",
title: "Sample Project"
},
{
_id: "13",
type: "Misc",
title: "Demo Project"
}
]
},
{
_id: "2",
solutionTitle: "Another Solution",
projects: [
{
_id: "21",
type: "Java",
title: "Another Java Project"
}
]
}]
Now, I aim to retrieve all projects
of a specific type, such as Java
. I attempted the following query
using aggregation:
db.Solutions.aggregate (
{ "$unwind": "$projects" },
{ "$match": {"projects.type": "Java" } },
{ "$project": {"projects" : 1, "_id": 0, "solutionTitle": 0 } }
)
While this query works correctly, the result is not in the format I anticipated. The output looks like:
{
projects: {
_id: "12",
type: "Java",
title: "Sample Project"
},
projects: {
_id: "21",
type: "Java",
title: "Another Java Project"
}
}
Is there a way to structure the result as a list of projects, for instance:
[
{ _id: "12", type: "Java", title: "Sample Project" }
...
]
I have reviewed this SO thread and this one but they do not exactly address my requirement. Any assistance would be highly appreciated.