AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$replaceAll (표현식 연산자)

$replaceAll

입력 문자열에 있는 검색 문자열의 모든 인스턴스를 대체 문자열로 바꿉니다.

$replaceAll 대/소문자를 구분하고 발음 구별 부호를 구분하며 collection에 있는 모든 데이터 정렬을 무시합니다.

The $replaceAll operator has the following operator expression syntax:

{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }
필드
설명

The string on which you wish to apply the find. Can be any valid expression that resolves to a string or a null. If input refers to a field that is missing, $replaceAll returns null.

지정된 입력 내에서 검색 할 문자열입니다. 문자열 또는 로 해석되는 모든 유효한 표현식 일 수 있습니다. null find가 누락된 필드 참조하는 경우$replaceAll 은 를 null 반환합니다.

The string to use to replace all matched instances of find in input. Can be any valid expression that resolves to a string or a null.

입력, 찾기대체 표현식은 null 문자열 $replaceAll 또는 로 평가되어야 하며, 그렇지 않으면 이 오류로 인해 실패합니다.

If input or find refer to a field that is missing, they return null.

If any one of input, find, or replacement evaluates to a null, the entire $replaceAll expression evaluates to null:

예시
결과

{ $replaceAll: { input: null, find: "abc", replacement: "ABC" } }

null

{ $replaceAll: { input: "abc", find: null, replacement: "ABC" } }

null

{ $replaceAll: { input: "abc", find: "abc", replacement: null } }

null

$replaceAll 표현식에 대한 문자열 일치는 항상 대소문자를 구분하고 발음 부호를 구분합니다. 과 문자열 비교를 수행할 때 구성된 모든 데이터 정렬은 무시됩니다. $replaceAll

예를 들어 데이터 정렬 강도가 1인 샘플 collection을 생성합니다.

db.createCollection( "restaurants", { collation: { locale: "fr", strength: 1 } } )

1의 데이터 정렬 강도는 기본 문자만 비교하고 대소문자 및 분음 부호와 같은 다른 차이점을 무시합니다.

다음으로 예시 문서를 삽입합니다.

db.restaurants.insertMany( [
{ _id: 1, name: "cafe" },
{ _id: 2, name: "Cafe" },
{ _id: 3, name: "café" }
] )

다음 $replaceAll 작업은 name 필드 에 있는 "Cafe" 의 모든 인스턴스를 찾아서 바꾸려고 시도합니다.

db.restaurants.aggregate( [
{
$addFields:
{
resultObject: {
$replaceOne: {
input: "$name",
find: "Cafe",
replacement: "CAFE"
}
}
}
}
] )
{ "_id" : 1, "name" : "cafe", "resultObject" : "cafe" }
{ "_id" : 2, "name" : "Cafe", "resultObject" : "CAFE" }
{ "_id" : 3, "name" : "café", "resultObject" : "café" }

Because $replaceAll ignores the collation configured for this collection, the operation only matches the instance of "Cafe" in document 2:

{ _id: 1, name: "cafe", resultObject: "cafe" }
{ _id: 2, name: "Cafe", resultObject: "CAFE" }
{ _id: 3, name: "café", resultObject: "café" }

$match 와 같이 데이터 정렬을 존중하는 연산자는 이 컬렉션의 데이터 정렬 강도가 1이므로 "Cafe" 에 대해 문자열 비교를 수행할 때 세 문서 모두와 일치합니다.

The $replaceAll aggregation expression does not perform any unicode normalization. This means that string matching for all $replaceAll expressions will consider the number of code points used to represent a character in unicode when attempting a match.

예를 들어 문자 é은(는) 한 개 또는 두 개의 코드 점을 사용하여 유니코드로 표현할 수 있습니다.

Unicode
다음과 같이 표시됩니다.
코드 포인트

\xe9

é

1 ( \xe9 )

e\u0301

é

2 ( e + \u0301 )

Using $replaceAll with a find string where the character é is represented in unicode with one code point will not match any instance of é that uses two code points in the input string.

The following table shows whether a match occurs for a find string of "café" when compared to input strings where é is represented by either one code point or two. The find string in this example uses one code point to represent the é character:

예시
match

{ $replaceAll: { input: "caf\xe9", find: "café", replacement: "CAFE" } }

{ $replaceAll: { input: "cafe\u0301", find: "café", replacement: "CAFE" } }

no

Because $replaceAll does not perform any unicode normalization, only the first string comparison matches, where both the find and input strings use one code point to represent é.

다음 문서로 inventory collection을 생성합니다.

db.inventory.insertMany( [
{ _id: 1, item: "blue paint" },
{ _id: 2, item: "blue and green paint" },
{ _id: 3, item: "blue paint with blue paintbrush" },
{ _id: 4, item: "blue paint with green paintbrush" },
] )

다음 예시에서는 item 필드에 있는 'blue paint'의 각 인스턴스를 'red paint'로 바꿉니다.

db.inventory.aggregate([
{
$project:
{
item: { $replaceAll: { input: "$item", find: "blue paint", replacement: "red paint" } }
}
}
])

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

{ _id: 1, item: "red paint" }
{ _id: 2, item: "blue and green paint" }
{ _id: 3, item: "red paint with red paintbrush" }
{ _id: 4, item: "red paint with green paintbrush" }
이 페이지 평가하기

이 페이지의 내용