[mongodb] SQL to Aggregation Mapping Chart 몽고디비 SQL 쿼리 매핑

 

SQL to Aggregation Mapping Chart

On this page

The aggregation pipeline allows MongoDB to provide native aggregation capabilities that corresponds to many common data aggregation operations in SQL.

The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDB aggregation operators:

SQL Terms, Functions, and Concepts MongoDB Aggregation Operators
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT()
join $lookup
SELECT INTO NEW_TABLE $out
MERGE INTO TABLE $merge (Available starting in MongoDB 4.2)
UNION ALL $unionWith (Available starting in MongoDB 4.4)

For a list of all aggregation pipeline and expression operators, see Aggregation Pipeline Quick Reference.

SEE ALSO

SQL to MongoDB Mapping Chart

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

Examples

The following table presents a quick reference of SQL aggregation statements and the corresponding MongoDB statements. The examples in the table assume the following conditions:

  • The SQL examples assume two tables, orders and order_lineitem that join by the order_lineitem.order_id and the orders.id columns.

  • The MongoDB examples assume one collection orders that contain documents of the following prototype:

    {
      cust_id: "abc123",
      ord_date: ISODate("2012-11-02T17:04:11.102Z"),
      status: 'A',
      price: 50,
      items: [ { sku: "xxx", qty: 25, price: 1 },
               { sku: "yyy", qty: 25, price: 1 } ]
    }
    
SQL Example MongoDB Example Description
 
SELECT COUNT(*) AS count
FROM orders
 
db.orders.aggregate( [
   {
     $group: {
        _id: null,
        count: { $sum: 1 }
     }
   }
] )
Count all records from orders
 
SELECT SUM(price) AS total
FROM orders
 
db.orders.aggregate( [
   {
     $group: {
        _id: null,
        total: { $sum: "$price" }
     }
   }
] )
Sum the price field from orders
 
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_id, sum the price field.
 
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
ORDER BY total
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   },
   { $sort: { total: 1 } }
] )
For each unique cust_id, sum the price field, results sorted by sum.
 
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id,
         ord_date
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        },
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_idord_date grouping, sum the price field. Excludes the time portion of the date.
 
SELECT cust_id,
       count(*)
FROM orders
GROUP BY cust_id
HAVING count(*) > 1
 
db.orders.aggregate( [
   {
     $group: {
        _id: "$cust_id",
        count: { $sum: 1 }
     }
   },
   { $match: { count: { $gt: 1 } } }
] )
For cust_id with multiple records, return the cust_id and the corresponding record count.
 
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id,
         ord_date
HAVING total > 250
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        },
        total: { $sum: "$price" }
     }
   },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_idord_date grouping, sum the price field and return only where the sum is greater than 250. Excludes the time portion of the date.
 
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
 
db.orders.aggregate( [
   { $match: { status: 'A' } },
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   }
] )
For each unique cust_id with status A, sum the price field.
 
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
 
db.orders.aggregate( [
   { $match: { status: 'A' } },
   {
     $group: {
        _id: "$cust_id",
        total: { $sum: "$price" }
     }
   },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_id with status A, sum the price field and return only where the sum is greater than 250.
 
SELECT cust_id,
       SUM(li.qty) as qty
FROM orders o,
     order_lineitem li
WHERE li.order_id = o.id
GROUP BY cust_id
 
db.orders.aggregate( [
   { $unwind: "$items" },
   {
     $group: {
        _id: "$cust_id",
        qty: { $sum: "$items.qty" }
     }
   }
] )
For each unique cust_id, sum the corresponding line item qty fields associated with the orders.
 
SELECT COUNT(*)
FROM (SELECT cust_id,
             ord_date
      FROM orders
      GROUP BY cust_id,
               ord_date)
      as DerivedTable
 
db.orders.aggregate( [
   {
     $group: {
        _id: {
           cust_id: "$cust_id",
           ord_date: { $dateToString: {
              format: "%Y-%m-%d",
              date: "$ord_date"
           }}
        }
     }
   },
   {
     $group: {
        _id: null,
        count: { $sum: 1 }
     }
   }
] )
Count the number of distinct cust_idord_date groupings. Excludes the time portion of the date.
 

[출처] https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
공지 오라클 기본 샘플 데이터베이스 졸리운_곰 2014.01.02 86846
공지 [SQL컨셉] 서적 "SQL컨셉"의 샘플 데이타 베이스 SAMPLE DATABASE of ORACLE 가을의 곰을... 2013.02.10 79143
공지 [G_SQL] Sample Database 가을의 곰을... 2012.05.20 95889
61 [몽고디비 mongodb] MongoDB Bulk Insert – MongoDB insertMany file 졸리운_곰 2020.10.09 772
60 [mongodb 몽고디비] How to insert multiple document into a MongoDB collection using Java? 졸리운_곰 2020.10.09 1166
59 Mongodb Bulk operation 졸리운_곰 2020.10.09 957
58 MongoDB Bulk Write(대랑 쓰기) & Retryable Write(쓰기 재시도) file 졸리운_곰 2020.10.09 1349
57 How to work with MongoDB in .NET file 졸리운_곰 2020.09.30 1380
56 MongoDB : 기본 구조 file 졸리운_곰 2020.09.30 1126
» [mongodb] SQL to Aggregation Mapping Chart 몽고디비 SQL 쿼리 매핑 졸리운_곰 2020.09.23 1508
54 초간단 Mongo DB Quick Start Guide file 졸리운_곰 2020.09.20 920
53 MongoDB(몽고디비) 특징 정리 file 졸리운_곰 2019.12.25 1272
52 MongoDB의 한계(?) 졸리운_곰 2019.01.22 1182
51 MongoDB의 기본 CRUD 문법. 졸리운_곰 2018.12.30 1531
50 MongoDB를 쓰면서 알게 된 것들 file 졸리운_곰 2018.12.30 1593
49 [MongoDB] MongoDB의 제약사항들. (MongoDB limits thresholds) 졸리운_곰 2018.12.30 1508
48 몽고DB 컬렉션 관리 졸리운_곰 2018.12.30 1255
47 MongoDB 명령어 (database, collection, document, query, cursor, index) file 졸리운_곰 2018.12.30 1271
46 Neo4j 소개 file 졸리운_곰 2018.07.05 1331
45 [DB] neo4j를 정리하자. file 졸리운_곰 2018.07.05 1229
44 Neo4J - 그래프 데이터베이스 file 졸리운_곰 2018.07.05 1475
43 MongoDB CRUD 동작의 이해 졸리운_곰 2018.06.26 1264
42 NoSQL 데이타 모델링 #1 file 졸리운_곰 2018.04.15 1639
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED