Please go through this my question:
MongoDB $ group and explicit group formation with computed column
But this time I need to compare strings, not numbers. The CASE request must have LIKE :
CASE WHEN source LIKE '%Web%' THEN 'Web'
Then I need to group by source. How to write this in Mongo? I am trying to do the following, but not sure if $regex supported inside $cond . By the way, is there a list of valid operators inside $cond somewhere? It looks like $cond doesn't really like me :)
db.Twitter.aggregate( { $project: { "_id":0, "Source": { $cond: [ { $regex:['$source','/.* Android.*/'] }, 'Android', { $cond: [ { $eq: ['$source', 'web'] }, 'Web', 'Others' ] } ] } } } );
There are many other meanings that I need to write there, making a deeper nesting. This is just an example with the simplicity of “Android” and “Web” for short. I tried both with $eq and $regex . Using $regex gives an invalid operator error, while using $eq does not understand the regex expression and puts everything under "Other". If possible with a regex, kindly tell me how to write it for case insensitivity.
Thanks for any help :-)
regex mongodb mongodb-query
Aafreen sheikh
source share