I have mongodb documents in the following format:
{
"date": "04-10-2018",
"botData": [{
"botId": "botId1",
"date": "04-10-2018",
"weekDay": "Thursday",
"hourOfDayData": {
"09": 24,
"11": 56,
"02": 66
},
"channelData": [{
"channel": "web",
"totalTraffic": 185,
"totalMessagesSent": 357,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 4.34
},
{
"channel": "fb",
"totalTraffic": 32,
"totalMessagesSent": 78,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 2.34
}
]
}, {
"botId": "botId2",
"date": "04-10-2018",
"weekDay": "Thursday",
"hourOfDayData": {
"09": 24,
"11": 56,
"02": 66
},
"channelData": [{
"channel": "web",
"totalTraffic": 185,
"totalMessagesSent": 357,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 4.34
},
{
"channel": "fb",
"totalTraffic": 32,
"totalMessagesSent": 78,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 2.34
}
]
}]}
I want to query part of the document. I want a query which will give me the following result:
{
"botId": "botId1",
"date": "04-10-2018",
"weekDay": "Thursday",
"hourOfDayData": {
"09": 24,
"11": 56,
"02": 66
},
"channelData": [{
"channel": "web",
"totalTraffic": 185,
"totalMessagesSent": 357,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 4.34
},
{
"channel": "fb",
"totalTraffic": 32,
"totalMessagesSent": 78,
"avgMessagesSentPerSession": 1.93,
"averageInteractionTime": 2.34
}
]
}
This is the query that I'm using now:
db.test.find({botData:{$elemMatch:{botId:'botId1'}}},{"botData":1,_id:0})
For which I'm getting the following result:
{
"botData" : [
{
"botId" : "botId1",
"date" : "04-10-2018",
"weekDay" : "Thursday",
"channelData" : [
{
"channel" : "web",
"totalTraffic" : 185.0,
"totalMessagesSent" : 357.0,
"avgMessagesSentPerSession" : 1.93,
"averageInteractionTime" : 4.34
},
{
"channel" : "fb",
"totalTraffic" : 32.0,
"totalMessagesSent" : 78.0,
"avgMessagesSentPerSession" : 1.93,
"averageInteractionTime" : 2.34
}
]
},
{
"botId" : "botId2",
"channelData" : [
{
"channel" : "web",
"totalTraffic" : 185.0,
"totalMessagesSent" : 357.0,
"avgMessagesSentPerSession" : 1.93,
"averageInteractionTime" : 4.34
},
{
"channel" : "fb",
"totalTraffic" : 32.0,
"totalMessagesSent" : 78.0,
"avgMessagesSentPerSession" : 1.93,
"averageInteractionTime" : 2.34
}
]
}
]
}
I have tried mongodb projections feature as you can see. But that didnt work as I wanted it to.
Can anyone please find a suitable query to get the result that I wanted?