We are new to Mongodb, as part of our POC, we ingested a bunch of documents into Mongo and testing out different query and projection options for data retrieval. One of the use cases, we are trying to filtering below sample document in Mongo and get only sub document. Just like how we get data in a relational db.
Sample Input.
{
"_id": "59036b0fa036cc28c8e07db6",
"sections": [{
"srcName": "test1",
"CompanyName": "ABC",
"sectorCode": "P",
"raw": false,
"data": [{
"srcKey": "",
"rowIdx": 0
},
{
"srcKey": "01",
"rowIdx": 1
}
]
}]
}
**desired output**
{
"_id" : ObjectId("59036b0fa036cc28c8e07db6"),
"sections" : [
{
"srcName" : "test1",
"CompanyName" : "ABC",
"sectorCode" : "P",
"raw" : false,
"data" : [
{
"srcKey" : "01",
"rowIdx" : 1
}
]
}
]
}
The Shell command I used in RoboMongo is
1/db.getCollection('InsStatData').aggregate([
{"$project": {
"sections": {
"$filter": {
"input": "$sections", "as": "data", "cond": { "setIsSubset": [["$$data.rowIdx"], [1]] }}}}}])
2/ db.getCollection('InsStatData').aggregate({$match: {"sections.data.rowIdx": 1} })
Please suggest how to desired output.