0

This is a sample document that I have:

{
"sessionId" : "98e9be38-3ff9-454a-aef4-625acaadbb62",
"userMessage" : "Liked It :)",
"hour" : "14"
},
{
"sessionId" : "98e9be38-3ff9-454a-aef4-625acaadbb62",
"userMessage" : "Hated It :(",
"hour" : "14"
},
{
"sessionId" : "98e9be38-3ff9-454a-aef4-625acaadbb62",
"userMessage" : "Just okay, could be better",
"hour" : "14"
}      
{
"sessionId" : "98e9be38-3ff9-454a-aef4-625acaadbb62",
"userMessage" : "Sample Message",
"hour" : "14"
},
{
"sessionId" : "98e9be38-3ff9-454a-aef4-625acaadbb62",
"userMessage" : "Sample message 2",
"hour" : "14"
} 

I want to count the number of documents which matches userMessages to 'Liked It :)' or 'Hated It :(' or 'Just okay, could be better'.

Expected output:

{[
   "Liked It :)":5,
   "Hated It :(":3,
   "Just okay, could be better":1
]}  

What I have tried:

db.getCollection('messagelogs').aggregate([
{"$group" : {_id:"$userMessage", count:{$sum:1}}}
])  

But the above query groups all the 'userMessage' field which is not what I want, I want to group only the 'userMessage' fields with values 'Liked It :)', 'Hated It :(', and 'Just okay, could be better'

Tony Mathew
  • 880
  • 1
  • 12
  • 35
  • @Fanpark, I have updated the question, can you please have a look? The question you have marked as duplicate does not give what I want, please check and let me know – Tony Mathew Jun 06 '19 at 10:31
  • Tony Please ask a new question if you think there is ambiguity in the existing one. But for now I cannot see any difference in your question and the duplicate answer. You just need to use one `$match` stage at the begining with the `$in` expression. Something like `{ $match: { userMessage: { $in: [ 'Liked It :)', 'Hated It :(', 'Just okay' ] }}}` – Ashh Jun 06 '19 at 11:17

0 Answers0