Mongodb uses aggregate, group, and match to implement the having (count (1) (1) function in mysql.

Source: Internet
Author: User

Mongodb uses aggregate, group, and match to implement the having (count (1) (1) function in mysql.

In relational databases, group deduplication is generally set to group... Having (count (1)> 1 )... The record group is selected for the competition, and an SQL statement is executed. However, it is not so convenient in mongodb. You need to write your own scripts to achieve this, you can perform operations through aggregate, group, and match.

1. Prepare to input test data

db.stu.insert ({cid: 1, age: 14, name: 'gom1'});
db.stu.insert ({cid: 1, age: 12, name: 'jack2'});
db.stu.insert ({cid: 2, age: 13, name: 'Lily3'});
db.stu.insert ({cid: 2, age: 14, name: 'tony4'});
db.stu.insert ({cid: 2, age: 9, name: 'Harry5'});
db.stu.insert ({cid: 2, age: 13, name: 'Vincent6'});
db.stu.insert ({cid: 1, age: 14, name: 'bill7'});
db.stu.insert ({cid: 2, age: 17, name: 'tim8'});
db.stu.insert ({cid: 1, age: 10, name: 'bruce9'});
db.stu.insert ({cid: 3, age: 20, name: 'luxi10'});
The execution window is displayed as follows:

mongos> use test;
switched to db test
mongos>
mongos> db.stu.insert ({cid: 1, age: 14, name: 'gom1'});
mongos> db.stu.insert ({cid: 1, age: 12, name: 'jack2'});
mongos> db.stu.insert ({cid: 2, age: 13, name: 'Lily3'});
mongos> db.stu.insert ({cid: 2, age: 14, name: 'tony4'});
mongos> db.stu.insert ({cid: 2, age: 9, name: 'Harry5'});
mongos> db.stu.insert ({cid: 2, age: 13, name: 'Vincent6'});
mongos> db.stu.insert ({cid: 1, age: 14, name: 'bill7'});
mongos> db.stu.insert ({cid: 2, age: 17, name: 'tim8'});
mongos> db.stu.insert ({cid: 1, age: 10, name: 'bruce9'});
mongos> db.stu.insert ({cid: 3, age: 20, name: 'luxi10'});
2. Start grouping test, group by cid 2.1, first count the number of group records
var group = ([[
{group: {_ id: "cid", max_age: {max: "age"}, count: {sum: 1}}},
{sort: {count: -1}}
])
The execution window is as follows:

mongos> var group = ([[
... {$ group: {_ id: "$ cid", max_age: {$ max: "$ age"}, count: {$ sum: 1}}},
... {$ sort: {count: -1}}
...])
mongos>
2.2. The definition is to find out that there are duplicate groups, using the pipeline operator match, the condition is the format of the ordinary query, but the format of the output result of the group:
var match = {"match": {"count": {"gt": 1}}};
The execution window is as follows:

mongos> var match = {"$ match": {"count": {"$ gt": 1}}};
mongos>
3 Get data
mongos> db.stu.aggregate (group, match);
{
    "result": [
        {
            "_id": 2,
            "max_age": 17,
            "count": 5
        },
        {
            "_id": 1,
            "max_age": 14,
            "count": 4
        },
        {
            "_id": 3,
            "max_age": 20,
            "count": 1
        }
    ],
    "ok": 1
}
mongos>
PS: The match is invalid here, the data with the count of 1 comes out, no match is successful, what is the problem?

Netizen Traveller's advice:
db.stu.aggregate (group, match); Can you see it? Your call is equivalent to passing match as an option. Of course it is useless. This method does not get the final data, because each pipe is an element of the array, and the group is an array alone, and match is an element. Unless you add it to the array, see the following writing,
group.push (match);
db.stu.aggregate (group);

Then go to the official website for further research:
http://docs.mongoing.com/manual-zh/reference/method/db.collection.aggregate.html#db.collection.aggregate, check the introduction of aggregate, the method signature of aggregate is function (pipeline, aggregateOptions)
The first parameter is piplines, all the judgment conditions for the data should be in it.
The second parameter is options, which specifies some options used during the query.
The options document can contain the following fields and values: including explain, allowDiskUse, cursor, so the match I passed is a judgment condition for the group, so it cannot be used as the second parameter, but should be put in the first parameter, Therefore, you need to use the push function of the group and add match to the first parameter to make the count> 1 judgment effective.

4. Use the push function of the group
mongos> group.push (match);
3
mongos> db.stu.aggregate (group);
{
    "result": [
        {
            "_id": 2,
            "max_age": 17,
            "count": 5
        },
        {
            "_id": 1,
            "max_age": 14,
            "count": 4
        }
    ],
    "ok": 1
}
mongos>
Seeing that the record group with count = 1 has been filtered out, it means that the push is successful

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.