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

$setField(표현식 연산자)

$setField

버전 5.0의 신규 항목입니다.

문서에서 지정된 필드를 추가, 업데이트 또는 제거합니다.

를 사용하여 마침표()가 포함되거나 달러 $setField 기호()로 시작하는 이름의 필드를 추가, 업데이트 또는 제거 수.$ 있습니다.

일부 쿼리 처리 컨텍스트에서 이러한 필드가 제거되거나 액세스할 수 없을 수 있으므로 예약된 내부 메타데이터 필드 이름과 충돌되는 루트 수준의 달러 접두사 필드 이름을 사용하지 마세요. 달러 기호($)나 마침표(.)가 포함된 필드 이름의 제한 사항 및 지침은 마침표 및 달러 기호가 포함된 필드 이름을 참조하세요.

$getField로 추가하거나 업데이트 달러 기호()$ 또는 마침표()가 포함된 필드의 값을 조회. $setField하려면 를 사용합니다.

$setField 의 구문은 다음과 같습니다:

{
$setField: {
field: <String>,
input: <Object>,
value: <Expression>
}
}

다음 필드를 제공해야 합니다.

필드
유형
설명

field

문자열

추가, 업데이트 또는 제거하려는 input 객체의 필드입니다. field는 문자열 상수로 해석되는 모든 유효한 표현식이 될 수 있습니다.

input

객체

추가하거나 업데이트하려는 field가 포함된 문서입니다. input은 객체, missing, null 또는 undefined로 해석되어야 합니다.

value

표현식

.field에 할당하려는 값입니다. value는 임의의 유효한 표현식일 수 있습니다.

input 문서에서 field를 제거하려면 $$REMOVE로 설정합니다.

  • input missingundefined가, 또는 null$setField 평가되면 는 를 null 반환하고 를 업데이트 하지 input 않습니다.

  • input가 객체,, missing undefined또는 이외의 것으로 평가되면 null $setField는 오류를 반환합니다.

  • field이 문자열 상수$setField 이외의 것으로 해석되는 경우 는 오류를 반환합니다.

  • 에 이 field 없으면 input $setField가 이를 추가합니다.

  • $setField 객체나 배열을 암시적으로 순회하지 않습니다. 예시 를$setField 들어 field 는 의 값을 "a.b.c" "a.b.c" 중첩된 필드 가 아닌 최상위 필드 로 평가합니다. { "a": { "b": { "c": } } }

  • $unsetField는 입력 값이 인 $setField$$REMOVE 별칭입니다. 다음 표현식은 이에 해당합니다.

    {
    $setField: {
    field: <field name>,
    input: "$$ROOT",
    value: "$$REMOVE"
    }
    }
    {
    $unsetField: {
    field: <field name>,
    input: "$$ROOT"
    }
    }

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

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", price: 45.99, qty: 300 },
{ _id: 2, item: "winter coat", price: 499.99, qty: 200 },
{ _id: 3, item: "sun dress", price: 199.99, qty: 250 },
{ _id: 4, item: "leather boots", price: 249.99, qty: 300 },
{ _id: 5, item: "bow tie", price: 9.99, qty: 180 }
] )

다음 작업은 $replaceWith 파이프라인 단계와 $setField 연산자 사용하여 각 문서 에 새 필드"price.usd" 추가합니다. 값은 "price.usd" "price" 각 문서 의 값과 같습니다. 마지막으로 이 작업은 파이프라인 단계를 $unset 사용하여 "price" 필드 제거 .

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
]

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

db.inventory.insertMany( [
{ _id: 1, item: "sweatshirt", price: 45.99, qty: 300 },
{ _id: 2, item: "winter coat", price: 499.99, qty: 200 },
{ _id: 3, item: "sun dress", price: 199.99, qty: 250 },
{ _id: 4, item: "leather boots", price: 249.99, qty: 300 },
{ _id: 5, item: "bow tie", price: 9.99, qty: 180 }
] )

다음 작업은 $replaceWith 파이프라인 단계와 $setField 및 연산자를 $literal 사용하여 각 문서 에 새 필드 추가합니다. "$price" "$price" 값은 "price" 각 문서 의 값과 같습니다. 마지막으로 이 작업은 파이프라인 단계를 $unset 사용하여 "price" 필드 제거 .

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$price"
} } },
{ $unset: "price" }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
]

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

db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

다음 작업은 $match 파이프라인 단계를 사용하여 특정 문서 찾고 $replaceWith 파이프라인 단계와 연산자 $setField 사용하여 일치하는 문서 의 필드 업데이트 "price.usd" .

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: 49.99
} } }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 49.99 }
]

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

db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )

다음 작업은 $match 파이프라인 단계를 사용하여 특정 문서 찾고,$replaceWith 파이프라인 단계와 $setField$literal 연산자를 사용하여 일치하는 문서 의 "$price" 필드 업데이트 .

db.inventory.aggregate( [
{ $match: { _id: 1 } },
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: 49.99
} } }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 49.99 }
]

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

db.inventory.insertMany([
{ _id: 1, item: 'sweatshirt', qty: 300, 'price.usd': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, 'price.usd': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, 'price.usd': 9.99 }
] )

다음 작업은 $replaceWith 파이프라인 단계와 연산자 및 $setField$$REMOVE 사용하여 "price.usd" 각 문서 에서 필드 제거 .

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: "price.usd",
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

$unsetField 별칭을 사용하여 작성된 유사한 쿼리는 동일한 결과를 반환합니다.

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: "price.usd",
input: "$$ROOT"
} } }
] )

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

db.inventory.insertMany( [
{ _id: 1, item: 'sweatshirt', qty: 300, '$price': 45.99 },
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
{ _id: 3, item: 'sun dress', qty: 250, '$price': 199.99 },
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 },
{ _id: 5, item: 'bow tie', qty: 180, '$price': 9.99 }
] )

다음 작업은 $replaceWith 파이프라인 단계, 및 연산자,$setField $literal$$REMOVE 사용하여 "$price" 각 문서 에서 필드 제거 .

db.inventory.aggregate( [
{ $replaceWith: {
$setField: {
field: { $literal: "$price" },
input: "$$ROOT",
value: "$$REMOVE"
} } }
] )

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

[
{ _id: 1, item: 'sweatshirt', qty: 300 },
{ _id: 2, item: 'winter coat', qty: 200 },
{ _id: 3, item: 'sun dress', qty: 250 },
{ _id: 4, item: 'leather boots', qty: 300 },
{ _id: 5, item: 'bow tie', qty: 180 }
]

$unsetField 별칭을 사용하여 작성된 유사한 쿼리는 동일한 결과를 반환합니다.

db.inventory.aggregate( [
{ $replaceWith: {
$unsetField: {
field: { $literal: "$price" },
input: "$$ROOT"
} } }
] )
이 페이지 평가하기

이 페이지의 내용