Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ MongoDB uses aggregate, group, match mysql achieve in having (count (1)> 1) features     - To create a problem by the statement date comparison of the difference between MySQL and Oracle (Database)

- Dynamic programming Android (Programming)

- Setting CentOS firewall open port (Linux)

- The most simple drive to write and test procedures under linux (Programming)

- Java Performance Tuning the proxy mode (Programming)

- CentOS 5.3 under broadcom NIC dual activation issues (Linux)

- CentOS 6.x and CentOS7 install MPlayer (Linux)

- Linux Tutorial Share: How to sudo command to define the PATH environment variable (Linux)

- How to Install Node.js in CentOS 7 (Linux)

- Use lsof restore accidentally deleted log files or database (Linux)

- Mac OS X Server installation and application (Linux)

- Linux use iptables ban Ping (Linux)

- Linux operating system boot manager -GRUB (Linux)

- CentOS 6.5 can not connect to the network under VMware (Linux)

- Linux Getting Started tutorial: GNU C and Vim will fight the C / C ++ IDE semi-automatic (Linux)

- The virtual memory (Linux)

- The Samba service does not have permission to access (Server)

- Ubuntu Linux use ufw or iptables firewall configuration (Linux)

- Linux Getting Started Tutorial: Ubuntu laptop screen brightness adjustment (Linux)

- CentOS 6.6 installation certification system based on the ftp service (Server)

 
         
  MongoDB uses aggregate, group, match mysql achieve in having (count (1)> 1) features
     
  Add Date : 2018-11-21      
         
       
         
  Relational databases are generally grouped to re-group by ... having (count (1)> 1) ... Game elected duplicate set of records, then get a sql, but mongodb inside, not so convenient, you need to write your own script to achieve, can be operated by handle aggregate, group, match.

1, ready to enter 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'});
Execution window shown below:

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, the test start packet, grouped according to the cid

2.1, the first packet statistics records

var group = ([
{Group: {_ id: "cid", max_age: {max: "age"}, count: {sum: 1}}},
     {Sort: {count: -1}}
])
Execution window as follows:

mongos> var group = ([
... {$ Group: {_ id: "$ cid", max_age: {$ max: "$ age"}, count: {$ sum: 1}}},
... {$ Sort: {count: -1}}
...])
mongos>

1
2.2, the definition is to find duplicate packets, use the pipeline operator match, provided that the general format of the query, but the role of the group in the output format:

var match = { "match": { "count": { "gt": 1}}};
Execution window 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: this match is invalid, out count data 1, no match is successful, the problem lie?

Traveller pointing users:
db.stu.aggregate (group, match); see out of it? You call this match as equivalent to pass in options, of course, did not use this method are not the final data, because each pipe is an element of the array, and the group is a single array, match is an element. Unless add it to the array inside, see the following lines,
group.push (match);
db.stu.aggregate (group);

Then see for yourself the official website for further study:
http://docs.mongoing.com/manual-zh/reference/method/db.collection.aggregate.html#db.collection.aggregate, view the aggregate presentation, aggregate method signature function (pipeline, aggregateOptions)

The first parameter is piplines, all of the data to determine the conditions should be in there.
The second argument is options, specify the query options when in use.
The options document can contain the following fields and values: include explain, allowDiskUse, cursor, so my judgment is passed in match conditions for the group, it is not as the second argument, the first argument should be placed inside, so it is necessary to use the push function group, will match the first parameter is added inside to make the count> 1 judge takes effect.

4, using the group's push function

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>

See already filtered out groups of records count = 1, and represents a successful push.
     
         
       
         
  More:      
 
- Linux, ls command to achieve (Linux)
- Graphical development environment to build Android under Ubuntu 11.04 (Linux)
- Linux system monitoring tool set cpu (Linux)
- RHEL6.5 replace local YUM source (Linux)
- Linux system last command usage (Linux)
- How to use static, class, abstract method in Python (Programming)
- Linux kernel modules related to the management Comments (Linux)
- Oracle 11g maintenance partitions (eight) - Renaming Partitions (Database)
- C ++ 11 smart pointers (Programming)
- Thinking in Java study notes - initialization and cleanup (Programming)
- Several Ceph performance optimization of new methods and ideas (2015 SH Ceph Day after flu reference) (Server)
- Android Studio Installation and Configuration Guide tutorial (Linux)
- How to install Laravel PHP framework on CentOS 7 / Ubuntu 15.04 (Server)
- OGG-03510 Problem (Database)
- Using C ++ Container Templates in Shared Memory (Programming)
- Android design patterns - state mode (Programming)
- Android custom slideshow menu slidmenu (Programming)
- Open SSH remote access service that allows Android phone Ubuntu 14.04 (Linux)
- How to install and use the Snort in Ubuntu 15.04 (Linux)
- Oracle 12c In-Memory Study (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.