Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ MongoDB uses aggregate, group, match mysql achieve in having (count (1)> 1) features     - Spring Boot + Nginx + Tomcat + SSL configuration notes (Server)

- SQL Server automatic backup script (Database)

- Nginx configuration support f4v video format player (Server)

- VMware virtual machine can not start VMnet0 no Internet access and other issues (Linux)

- swap space is insufficient cause OOM kill MySQL Case (Database)

- Use in Linux ipmitool tool (Linux)

- When a software RAID data recovery and reconstruction of how failure (Linux)

- Workspace Go language and environment variables GOPATH (Linux)

- Construction CA certificate using OpenSSL command line (Server)

- Use Spring cache and ehcache (Programming)

- Java proxy mode (Programming)

- 10 Regulation of painless SQL Schema (Database)

- The method of MySQL two kinds of incomplete recovery (Database)

- Flask deploy applications using Nginx on Ubuntu (Server)

- Linux Network Programming --epoll model Detailed principles and examples (Programming)

- Linux batch copy data script (Linux)

- installation and configuration of the PHP environment (Apache2) under Linux (Server)

- B-tree - ideas and implementation of C language code (Programming)

- Installation and Configuration ISC DHCP server on Debian Linux (Server)

- Impact test noatime Linux file access time (Linux)

 
         
  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:      
 
- MySQL performance view and configure finishing Daquan (Database)
- Hanoi problem Java Solution (Programming)
- Grub2 Boots the openSUSE installation image (Linux)
- PostgreSQL procedural language learning (Database)
- CentOS 7.0 running Docker kernel error solution (Server)
- Ubuntu install video playback software SMPlayer 14.9.0.7042 (Linux)
- Linux System Tutorial: Ubuntu on the desktop is disabled by default keyring to unlock tips (Linux)
- Linux System Getting Started Learning: Linux in the last command (Linux)
- Questions about Linux compiler u-boot (Programming)
- Mongo-connector integrated MongoD to achieve incremental Solr index (Server)
- Linux directory permissions to read and execute permissions difference (Linux)
- Protobuf compiled and used on the Ubuntu 14.04 (Programming)
- Python several standard types of built-in functions (Programming)
- Oracle Client + PL SQL Developer enables remote access to the Oracle database (Database)
- The default permissions for files and directories under Linux computing (Linux)
- Wireless LAN security solutions (Linux)
- Number JavaScript type system (Programming)
- Shell Scripting early experience (Programming)
- Nginx is used to build the cache module srcache_nginx (Server)
- How to Install Linux kernel 4.0 on Ubuntu / CentOS (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.