MySQL and Mongo simple query instance code, mysqlmongo instance

Source: Internet
Author: User

MySQL and Mongo simple query instance code, mysqlmongo instance

First of all, I will not talk about the difference between RDS and non-rds (there are many on Baidu ).

The content I want to query is as follows: The score is greater than 0 and the average score of the person with the name bob or jake is the average score. The maximum score is the minimum score.

Let's take this example and try to write a query using MySQL and mongodb respectively.

First, make some preparations.

The MySQL database structure is as follows:

CREATE TABLE `new_schema`.`demo` (`id` INT NOT NULL,`person` VARCHAR(45) NOT NULL,`score` VARCHAR(45) NOT NULL,PRIMARY KEY (`id`));

After the table is created, We can insert some data.

INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('1', 'bob', '50');INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('2', 'jake', '60');INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('3', 'bob', '100');INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('6', 'jake', '100');INSERT INTO `new_schema`.`demo` (`id`, `person`, `score`) VALUES ('8', 'li', '100');

I will cut a graph to check the structure.

Okay. Next, let's go to mongodb preparation and take a look at the structure of the documents in the set of mongodb (basically the same as MySQL) here, I will not write the specific process of inserting a document. (To view the mongodb display, I use two formats: one is the cousin module, and the other is the text module)

This is the table module display

This is the text module display

/* 1 */{"_id" : ObjectId("58043fa8e9a7804c05031e17"),"person" : "bob","sorce" : 50}/* 2 */{"_id" : ObjectId("58043fa8e9a7804c05031e18"),"person" : "bob","sorce" : 100}/* 3 */{"_id" : ObjectId("58043fa8e9a7804c05031e19"),"person" : "jake","sorce" : 60}/* 4 */{"_id" : ObjectId("58043fa8e9a7804c05031e1a"),"person" : "jake","sorce" : 100}/* 5 */{"_id" : ObjectId("58043fa8e9a7804c05031e1b"),"person" : "li","sorce" : 100}

Start to start

The MySQL statement I want to query now is as follows (the score is greater than 0 and the name of the person is bob or Jacob's total score average score minimum score maximum score count)

SELECT person, SUM(score), AVG(score), MIN(score), MAX(score), COUNT(*) FROM demo WHERE score > 0 AND person IN('bob','jake') GROUP BY person;

The following code uses Mongo to write this query.

The first thing that comes to mind is the aggregation framework.

First use $ match to filter out a score greater than 0 with the name bob or jake.

db.demo.aggregate({"$match":{"$and":[{"sorce":{"$gt":0}},{"person":{"$in":["bob","jake"]}}]}}

Get this result

This is the result of the cousin module:

This is the result displayed by the text module:

/* 1 */{"_id" : ObjectId("58043fa8e9a7804c05031e17"),"person" : "bob","sorce" : 50}/* 2 */{"_id" : ObjectId("58043fa8e9a7804c05031e18"),"person" : "bob","sorce" : 100}/* 3 */{"_id" : ObjectId("58043fa8e9a7804c05031e19"),"person" : "jake","sorce" : 60}/* 4 */{"_id" : ObjectId("58043fa8e9a7804c05031e1a"),"person" : "jake","sorce" : 100}

Then you want to group and display the maximum, minimum, and total average values and counts.

Then $ group comes in handy:

db.demo.aggregate({"$match":{"$and":[{"sorce":{"$gt":0}},{"person":{"$in":["bob","jake"]}}]}},{"$group":{"_id":"$person","sumSorce":{"$sum":"$sorce"},"avgSorce":{"$avg":"$sorce"},"lowsetSorce":{"$min":"$sorce"},"highestSorce":{"$max":"$sorce"},"count":{"$sum":1}} })

The result is that the score is greater than 0 and the average score of the person with the name bob or jake is the minimum score and the maximum score count.

The result table module displays:

The text module of the result shows:

/* 1 */{"_id" : "bob","sumSorce" : 150,"avgSorce" : 75.0,"lowsetSorce" : 50,"highestSorce" : 100,"count" : 2.0}/* 2 */{"_id" : "jake","sumSorce" : 160,"avgSorce" : 80.0,"lowsetSorce" : 60,"highestSorce" : 100,"count" : 2.0}

The above is the simple query instance code of MySQL and Mongo introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.