I have multiple documents that needs to be queried from mongodb. Requirement is such that I need the nested array of documents has to be in a separate document under certain conditions. I have given a document sample for instance. In that I have an array of objects called cars
, the output have to be such that the status
is still inprogress
and the key: parent_company
is true
then the objects that has parent_company_id
equal to the _id
of the parent_company: true
and at least one of their model
array has v12
. This might be confusing when explaining but please check on the document in db and the expected result, you’d get an idea on the requirement here.
If you take a look at the second JSON below, you can see those are the same Documents with different array of cars with certain conditions and that is how am expecting the results to be from the actual data from a collection So the condition is status
has to be inprogress
for parent_company: true
and if any object under cars
array has v12
in model
then I need to get those documents that has the same parent_company_id
.
Document in db
[
{
"_id": ObjectId("63a8808652f40e1d48a3d1d7"),
"name": "A",
"description": null,
"cars": [
{
"id": "63a8808c52f40e1d48a3d1da",
"owner": "John Doe",
"purchase_date": "2022-12-25",
"status": "inprogress",
"parent_company": true,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"model": []
},
{
"id": "63a880a552f40e1d48a3d1dc",
"owner": "John Doe 1",
"purchase_date": "2022-12-25",
"parent_company": false,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"model": [
"v12"
]
},
{
"id": "63a880f752f40e1d48assddd",
"owner": "John Doe 1",
"purchase_date": "2022-12-25",
"parent_company": false,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"model": []
},
{
"id": "63a880f752f40e1d48a3d207",
"owner": "John Doe 11",
"dt": "2022-12-25",
"status": "inprogress",
"parent_company": true,
"parent_company_id": "63a880f752f40e1d48a3d207",
"model": [
"v12"
]
},
{
"id": "63a880f752f40e1d48agfdddgg",
"owner": "John Doe 112",
"dt": "2022-12-25",
"status": "inprogress",
"parent_company": true,
"parent_company_id": "63a880f752f40e1d48agfdddgg",
"model": []
}
]
}
]
Result am Expecting
[
{
"_id": ObjectId("63a8808652f40e1d48a3d1d7"),
"cars": [
{
"id": "63a8808c52f40e1d48a3d1da",
"model": [],
"owner": "John Doe",
"parent_company": true,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"purchase_date": "2022-12-25",
"status": "inprogress"
},
{
"id": "63a880a552f40e1d48a3d1dc",
"model": [
"v12"
],
"owner": "John Doe 1",
"parent_company": false,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"purchase_date": "2022-12-25"
},
{
"id": "63a880f752f40e1d48assddd",
"model": [],
"owner": "John Doe 1",
"parent_company": false,
"parent_company_id": "63a8808c52f40e1d48a3d1da",
"purchase_date": "2022-12-25"
}
],
"description": null,
"name": "A"
},
{
"_id": ObjectId("63a8808652f40e1d48a3d1d7"),
"cars": [
{
"dt": "2022-12-25",
"id": "63a880f752f40e1d48a3d207",
"model": [
"v12"
],
"owner": "John Doe 11",
"parent_company": true,
"parent_company_id": "63a880f752f40e1d48a3d207",
"status": "inprogress"
}
],
"description": null,
"name": "A"
}
]
the partition condition is such that - car array objects that has parent_company_id same and model has v12 in the array and if the parent_company is true then the status should in inprogress and also it’s id has to match with the parent_company_id from the above.