I have mongo documents as follows,
{
"_id" : ObjectId("5a5ee704a2032a61f3be28"),
"companyID" : "somevalue",
"Name" : "some name",
"facebookEvents" : [
{
"type" : "like",
"message" : "sdfdfdf",
"link" : "http://www.facebook.com/140",
"timeStamp" : 1431014457000.0,
},
{
"type" : "comment",
"message" : "jjjjmmmm",
"link" : "http://www.facebook.com/140",
"timeStamp" : 1431014457000.0
},
{
"type" : "like",
"message" : "Try our Android application",
"link" : "http://www.facebook.com/140",
"timeStamp" : 1431014457000.0
},
,
{
"type" : "like",
"message" : "Try our Android application",
"link" : "http://www.facebook.com/140",
"timeStamp" : 1431014457000.0
}
]
}
Is it possible to query documents that have more than two "type"="like" objects inside facebookEvents, for example, the above document is a valid result because it has more than two objects with "type"="like" inside `facebookEvents'. I tried the way as in this question explains, but it is not exactly what i want.
Appriciate any help
UPDATE: mongo version: 3.4.4
I tried using aggregation as follows,
db.getCollection('customerData').aggregate( [
{ $project : { 'companyID' : 1, 'facebookEvents' : 1,'Name':1 } },
{ $unwind : "$facebookEvents" },
{ $match : { 'facebookEvents.type' : { $eq: 'like' } } },
{ $group : { _id : "$_id", likes : { $sum : 1} }},
{$match:{likes:{$gt:2}}}
] )
it returns correct objects but not with all the fields in the original object. Appriciate if someone can help me to retrieve object with all the fields(Name,facebookEvents,companyID)