Convert SQL query to Mongodb

Asked

Viewed 652 times

0

I have little (or no) knowledge in non-relational banks. I need to convert the below query (Sqlite) into an equivalent query to be executed 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);

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

2 answers

1


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: https://klaus.dk/sqltomongodb/

  • So the Mongodb query is comparing the member away_team_goal with the string "home_team_goal", and vice versa

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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.