Database war
Mysql & Mongodb
Other Queries

Database war Mysql & Mongodb Other Queries

English

Carlos Rivas's photo
Carlos Rivas

Published on Mar 16, 2021

2 min read

Hey my friends, a new post about this war. I don't know which is better, but I'll try to figure it out. I hope y'all enjoy this series. Here we go!

This time I'm going to talk about a few queries that can help you!

Count

If you need to count how many registers exist with the same query.

Mysql

SELECT count(*) FROM crews WHERE captain = 'Luffy';

MongoDB

db.crews.find({"captain":"Luffy"}).count()

Sum

If you need to sum a specific field.

Mysql

 SELECT SUM(members) FROM crews;

MongoDB

db.crews.aggregate(
  {
    $match:{}
  },
  {
    $group: {
      _id: null, 
      sum: {
        $sum: "$members"
      }
    }
  });

Min

If you need to get the min field of a query.

Mysql

SELECT MIN(members) FROM crews;

MongoDB

db.crews.aggregate(
  {
    $match:{}
  },
  {
    $group: {
      _id: null, 
      min: {
        $min: "$members"
      }
    }
  });

Max

If you need to get the max field of a query.

Mysql

SELECT MAX(members) FROM crews;

MongoDB

db.crews.aggregate(
  {
    $match:{}
  },
  {
    $group: {
      _id: null, 
      max: {
        $max: "$members"
      }
    }
  });

Conclusion

  • Structure: Mysql is easier than MongoDB.

Would you like to learn about other things?

I hope you enjoy my post and remember that I am just a Dev like you!

 
Share this