문서 메뉴
문서 홈
/
MongoDB 매뉴얼
/ / /

$isNumber(집계)

이 페이지의 내용

  • 정의
  • 예제
$isNumber

$isNumber 지정된 표현식 이 다음 숫자 BSON types중 하나로 해석되는지 확인합니다.

$isNumber 반환합니다:

  • true 표현식이 숫자로 해석되는 경우입니다.

  • false 표현식이 다른 BSON type, null 또는 누락된 필드로 해석되는 경우입니다.

$isNumber 다음과 같은 연산자 표현식 구문이 있습니다.

{ $isNumber: <expression> }

인수는 유효한 표현식일 수 있습니다.

다음도 참조하세요.

examples.sensors 컬렉션에 대해 다음 작업을 실행하여 테스트 데이터를 채웁니다.

db.getSiblingDB("examples").sensors.insertMany([
{ "_id" : 1, "reading" : NumberDecimal(26.0) },
{ "_id" : 2, "reading" : NumberLong(25.0) },
{ "_id" : 3, "reading" : NumberInt(24) },
{ "_id" : 4, "reading" : 24.0 },
{ "_id" : 5, "reading" : "24" },
{ "_id" : 6, "reading" : [ NumberDecimal(26) ]}
])

다음 애그리게이션은 $addFields 애그리게이션 단계를 사용하여 각 문서에 다음 필드를 추가합니다.

  • isNumber - reading의 값이 정수, 소수, 이중 또는 긴 값인지를 나타냅니다.

  • type - BSON type이 reading임을 나타냅니다.

db.sensors.aggregate([{
$addFields : {
"isNumber" : { $isNumber : "$reading" },
"hasType" : {$type : "$reading"}
}
}])

애그리게이션 작업은 다음과 같은 결과를 반환합니다.

{ "_id" : 1, "reading" : NumberDecimal("26.0000000000000"), "isNum " : true, "type" : "decimal" }
{ "_id" : 2, "reading" : NumberLong(25), "isNum " : true, "type" : "long" }
{ "_id" : 3, "reading" : 24, "isNum " : true, "type" : "int" }
{ "_id" : 4, "reading" : 24, "isNum " : true, "type" : "double" }
{ "_id" : 5, "reading" : "24", "isNum " : false, "type" : "string" }
{ "_id" : 6, "reading" : [ NumberDecimal("26.0000000000000") ], "isNum " : false, "type" : "array" }

grades 컬렉션에는 학생 성적에 대한 데이터가 포함되어 있습니다. grade 필드에 문자열 문자 등급 또는 숫자 점수 값을 저장할 수 있습니다.

db.getSiblingDB("examples").grades.insertMany([
{
"student_id" : 457864153,
"class_id" : "M044",
"class_desc" : "Introduction to MongoDB 4.4",
"grade" : "A"
},
{
"student_id" : 457864153,
"class_id" : "M103",
"class_desc" : "Basic Cluster Administration",
"grade" : 3.0
},
{
"student_id" : 978451637,
"class_id" : "M320",
"class_desc" : "MongoDB Data Modeling",
"grade" : "C"
},
{
"student_id" : 978451637,
"class_id" : "M001",
"class_desc" : "MongoDB Basics",
"grade" : 4.0
}
])

다음 애그리게이션에서는 $addFields 단계를 사용하여 해당 코스의 숫자 성적 값이 포함된 points 필드를 추가합니다. 단계에서는 $cond 연산자를 사용하여 $isNumber 출력값을 기반으로 points 값을 설정합니다.

  • true인 경우 grades에는 이미 숫자 점수 값이 포함되어 있습니다. pointsgrades와 같게 설정합니다.

  • false인 경우 grades에는 문자열 문자 값이 포함됩니다. $switch를 사용하여 문자 성적을 해당 점수 값으로 변환하고 points에 할당합니다.

그런 다음 aggregation pipeline은 $group 단계를 사용하여 student_id를 기준으로 그룹화하고 학생의 average GPA를 계산합니다.

db.getSiblingDB("examples").grades.aggregate([
{
$addFields: {
"points" : {
$cond : {
if : { $isNumber : "$grade" },
then: "$grade" ,
else: {
$switch : {
branches: [
{ case: {$eq : ["$grade" , "A"]}, then : 4.0 },
{ case: {$eq : ["$grade" , "B"]}, then : 3.0 },
{ case: {$eq : ["$grade" , "C"]}, then : 2.0 },
{ case: {$eq : ["$grade" , "D"]}, then : 1.0 },
{ case: {$eq : ["$grade" , "F"]}, then : 0.0 }
]
}
}
}
}
}
},
{
$group : {
_id : "$student_id",
GPA : {
$avg : "$points"
}
}
}
])

aggregation pipeline은 해당 학생의 GPA 학점 평균을 사용하여 고유 student_id당 하나의 문서를 출력합니다.

{ "_id" : 457864153, "GPA" : 3.5 }
{ "_id" : 978451637, "GPA" : 3 }
← $isArray (애그리게이션)

이 페이지의 내용