ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

$strcasecmp (aggregation)

이 페이지의 내용

$strcasecmp

Performs case-insensitive comparison of two strings. Returns

  • 1 if first string is "greater than" the second string.

  • 0 if the two strings are equal.

  • -1 if the first string is "less than" the second string.

$strcasecmp의 구문은 다음과 같습니다.

{ $strcasecmp: [ <expression1>, <expression2> ] }

The arguments can be any valid 표현식 as long as they resolve to strings. For more information on expressions, see 표현식 연산자.

$strcasecmp은(는) ASCII 문자의 문자열에 대해서만 잘 정의된 동작을 보유합니다.

For a case sensitive comparison, see $cmp.

다음 문서가 포함된 inventory collection을 생각해 보세요.

db.inventory.insertMany( [
{ "_id" : 1, "item" : "ABC1", quarter: "13Q1", "description" : "product 1" },
{ "_id" : 2, "item" : "ABC2", quarter: "13Q4", "description" : "product 2" },
{ "_id" : 3, "item" : "XYZ1", quarter: "14Q2", "description" : null }
] )

The following operation uses the $strcasecmp operator to perform case-insensitive comparison of the quarter field value to the string "13q4":

db.inventory.aggregate(
[
{
$project:
{
item: 1,
comparisonResult: { $strcasecmp: [ "$quarter", "13q4" ] }
}
}
]
)

이 연산은 다음과 같은 결과를 반환합니다.

{ "_id" : 1, "item" : "ABC1", "comparisonResult" : -1 }
{ "_id" : 2, "item" : "ABC2", "comparisonResult" : 0 }
{ "_id" : 3, "item" : "XYZ1", "comparisonResult" : 1 }

이 페이지의 내용