|
|
예전 MongoDB 자료에 나오는 group(), db.eval(), mapReduce 는 지금 버전에서 없어졌거나 쓰지 않는다. 지금 쓰는 방식 기준으로 정리했다.
1. 개수 세기
|
|
db.person.countDocuments({}) // 조건에 맞는 정확한 개수 db.person.countDocuments({ name: "neo" }) db.person.estimatedDocumentCount() // 메타데이터로 빠르게 전체 개수 |
예전의 db.collection.count() 와 cursor.count() 는 deprecated 다.
2. 중복 없는 값 (distinct)
|
|
db.person.distinct("age") db.phones.distinct("components.number", { "components.number": { $lt: 5550005 } }) |
3. 집계 파이프라인
문서가 단계(stage)를 차례로 통과하며 걸러지고 바뀌는 구조다. 유닉스 파이프와 같은 생각이다. 예전 group() 명령(4.2 에서 제거)은 $group […]
|
|
SELECT db_name(st.dbid) DBName , object_schema_name(objectid, st.dbid) SchemaName , object_name(objectid, st.dbid) SPName , qs.total_elapsed_time , creation_time , last_execution_time , text FROM sys.dm_exec_query_stats qs CROSS APPLY sys.dm_exec_sql_text(qs.plan_handle)st JOIN sys.dm_exec_cached_plans cp ON qs.plan_handle = cp.plan_handle |
|
|
select year as '년', month as '월', count(*) as count from ( select YEAR(us.fd_dtcreate) as 'year', MONTH(us.fd_dtcreate) as 'month' from tb_ykfile as us where us.fd_dtCreate > '2020-08-01' and us.fd_dtCreate <= '2022-01-01' ) tb group by year, month order by year, month ; |
|
|
SELECT CONVERT(varCHAR(10),Dateadd(d, a.number,'2020-08-01 00:00:00'),120) AS dt ,Isnull(b.count, 0) AS count FROM master..spt_values a LEFT JOIN ( SELECT count(*) AS count, CONVERT(CHAR(10), b1.fd_dtcreate,120) AS fd_dtcreate FROM tb_ykfile b1 WITH(nolock) GROUP BY CONVERT(CHAR(10), b1.fd_dtcreate, 120) ) b ON CONVERT(CHAR(10), Dateadd(d, a.number,'2020-08-01 00:00:00'), 120) = b.fd_dtcreate WHERE TYPE = 'P' AND CONVERT(CHAR(10), DATEADD(D, NUMBER, '2020-08-01'), 120) <= '2022-01-01' order by dt ; |
|
링크
|