If I have the following documents in my collection
{ "_id" : 1, "domainName" : "test1.com", "hosting1" : "hostgator.com", "hosting2" : "hostgator.com", sID : 1}
{ "_id" : 2, "domainName" : "test2.com", "hosting1" : "cloud.google.com", "hosting2" : "aws.amazon.com", sID : 2}
{ "_id" : 3, "domainName" : "test3.com", "hosting1" : "aws.amazon.com", "hosting2" : "cloud.google.com", sID : 2}
Suppose I want to find "cloud.google.com" in hosting1 or hosting2
I would write a query like
db.chats.find({$or : [{ hosting1 : 'cloud.google.com'}, { hosting2 : 'cloud.google.com'}]}).pretty();
this will give me two entries as below
{ "_id" : 2, "domainName" : "test2.com", "hosting1" : "cloud.google.com", "hosting2" : "aws.amazon.com", sID : 2}
{ "_id" : 3, "domainName" : "test3.com", "hosting1" : "aws.amazon.com", "hosting2" : "cloud.google.com", sID : 2}
Suppose I want to find and group by "sID" field
Suppose I want to find "cloud.google.com" on hosting1 or hosting2, and then GROUPBY on "sID": 2 means
My result will be
{ "_id" : 2, "domainName" : "test2.com", "hosting1" : "cloud.google.com", "hosting2" : "aws.amazon.com", sID : 2}
How to write a request for my above requirement
My sql request will be
SELECT *
FROM chats
WHERE (hosting1 = 'cloud.google.com' OR hosting2 = 'cloud.google.com')
GROUPBY sID;
I went through the mongoDB $ group, but I could not get it to work
Could you give me an idea of how to achieve this. Your help is greatly appreciated. Thank.