Convert SQL query to MongoDB

0

I have little (or no) knowledge in non-relational banks. I need to convert the query below (SQLite) into an equivalent query to be run in MongoDB.

SELECT COUNT(*) FROM match_table
WHERE (home_team_api_id=9991 AND home_team_goal > away_team_goal)     
OR (away_team_api_id=9991 AND away_team_goal > home_team_goal);

As I understand it, MongoDB does not accept queries with grouping in parentheses, among other "incompatibilities". Is it possible to perform an equivalent query in MongoDB ?? Thanks for the help!

    
asked by anonymous 12.06.2018 / 19:12

2 answers

1

It would look like this:

db.match_table.find({
   "$or":[
      {
         "$and":[
            {
               home_team_api_id:9991
            },
            {
               "home_team_goal":{
                  "$gt":"away_team_goal"
               }
            }
         ]
      },
      {
         "$and":[
            {
               "away_team_api_id":9991
            },
            {
               "away_team_goal":{
                  "$gt":"home_team_goal"
               }
            }
         ]
      }
   ]
}).count()

You can use this link to help: link

    
12.06.2018 / 19:33
0
db.match_table.find({$or: [ [ {"home_team_api_id": 9991}, {$where: "this.home_team_goal > this.away_team_goal"} ], [ {"away_team_api_id": 9991}, {$where: "this.away_team_goal > this.home_team_goal"} ] ]}).count()
    
12.06.2018 / 19:32