'MongoDB Alternate of a Conditional SQL Groupby Query

I have a Sqlite3 database and I have this problem statement:

Problem Statement: 'Print counts of movies with at least 1000 votes, grouped by their rating bin. All movies with the same integer part of rating fall in the same rating bin.'

For this problem, I wrote the following SQL query (works perfectly) to query my Sqlite3 database:

WITH
tbl1 AS(
SELECT *
FROM Movies
WHERE Num_Votes>=1000
)

SELECT Avg_Rating%10 AS Rating_Bin, COUNT(Movie_Title) AS Num_Movies
FROM Movies
GROUP BY Avg_Rating%10

Now, I have a MongoDB local database and I want to solve this exact problem by querying MongoDB. So, what will be this query's MongoDB equivalent?



Solution 1:[1]

db.collection.aggregate([
  {
    $match: {
      Num_Votes: { $gt: 1000 }
    }
  },
  {
    $group: {
      _id: { $floor: "$Avg_Rating" },
      Num_Movies: { $sum: "$Num_Votes" }
    }
  }
])

mongoplayground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 YuTing