we have a nested json as follows:
{
"_id" : ObjectId("5a074fe0b9d31f246c102078"),
"projects" : [
{
"workstreams" : [
{
"cvids" : [
{
"opportunity" : {
"resources" : [
{
"utilisation" : 100,
"weekly_details" : [],
"fte_start_date" : "2017-11-13T00:00:00.000Z",
"fte_end_date" : "2017-11-27T00:00:00.000Z",
"fte_value" : 1
}
],
"utilisation" : 100,
"_id" : ObjectId("5a098fe0b9d31f246c102077")
},
"opportunity_cvid" : "33333",
"rpl_cvid" : "33333"
}
],
"workstream_name" : "Proj 1 WS1"
}
],
"project_name" : "Proj 1"
},
{
"workstreams" : [
{
"cvids" : [
{
"opportunity" : {
"resources" : [
{
"utilisation" : 100,
"weekly_details" : [],
"fte_start_date" : "2017-11-13T00:00:00.000Z",
"fte_end_date" : "2017-11-27T00:00:00.000Z",
"fte_value" : 1
}
],
"utilisation" : 100,
"_id" : ObjectId("5a098fe0b9d31f246c642077")
},
"opportunity_cvid" : "121212",
"rpl_cvid" : "121212"
}
],
"workstream_name" : "Proj2 WS2"
}
],
"project_name" : "Test Proj2"
},
{
"workstreams" : [
{
"cvids" : [
{
"opportunity" : {
"resources" : [
{
"utilisation" : 100,
"weekly_details" : [],
"fte_start_date" : "2017-11-13T00:00:00.000Z",
"fte_end_date" : "2017-11-27T00:00:00.000Z",
"fte_value" : 1
}
],
"utilisation" : 100,
"_id" : ObjectId("5a098fe0b9d31f306c102077")
},
"opportunity_cvid" : "33333",
"rpl_cvid" : "33333"
},
{
"opportunity" : {
"resources" : [
{
"utilisation" : 100,
"weekly_details" : [],
"fte_start_date" : "2017-11-13T00:00:00.000Z",
"fte_end_date" : "2017-11-27T00:00:00.000Z",
"fte_value" : 1
}
],
"utilisation" : 100,
"_id" : ObjectId("5a741fe0b9d31f306c102077")
},
"opportunity_cvid" : "656565",
"rpl_cvid" : "656565"
}
],
"workstream_name" : "Proj3 WS 3"
}
],
"project_name" : "Test Proj 3"
}
],
"contract_name" : "Contract 1"}
I am fairly new to MongoDB. I want to make a generic query to find a list of all 'workstream_name' for the given project_name and contract_name.
For e.g for contract_name='Contract 1'(Top level array) and project_name='Test Proj2'(level 2 array) the output should be:
["Proj2 WS2"]
I have tried many queries. The closest I could get is:
db.getCollection('opportunity').distinct("projects.workstreams.workstream_name", {'contract_name': 'ADRO', 'projects.project_name': 'Proj 1'})
Output:
[
"Proj2 WS2",
"Proj3 WS 3",
"Proj 1 WS1"
]
In this output, the last two values do not match the criteria. Kindly suggest how the query can be improved to get only the workstream_names that match the criteria.