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

$setField(표현식 연산자)

$setField

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

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

You can use $setField to add, update, or remove fields with names that contain periods (.) or start with dollar signs ($).

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

Use $getField to retrieve the values of fields that contain dollar signs ($) or periods (.) that you add or update with $setField.

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

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

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

필드
유형
설명

field

문자열

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

input

객체

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

value

표현식

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

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

  • If input evaluates to missing, undefined, or null, $setField returns null and does not update input.

  • If input evaluates to anything other than an object, missing, undefined, or null, $setField returns an error.

  • If field resolves to anything other than a string constant, $setField returns an error.

  • If field doesn't exist in input, $setField adds it.

  • $setField doesn't implicitly traverse objects or arrays. For example, $setField evaluates a field value of "a.b.c" as a top-level field "a.b.c" instead of as a nested field, { "a": { "b": { "c": } } }.

  • $unsetField is an alias for $setField with an input value of $$REMOVE. The following expressions are equivalent:

    {
    $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 }
] )

The following operation uses the $replaceWith pipeline stage and the $setField operator to add a new field to each document, "price.usd". The value of "price.usd" equals the value of "price" in each document. Finally, the operation uses the $unset pipeline stage to remove the "price" field.

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 }
] )

The following operation uses the $replaceWith pipeline stage and the $setField and $literal operators to add a new field to each document, "$price". The value of "$price" equals the value of "price" in each document. Finally, the operation uses the $unset pipeline stage to remove the "price" field.

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 }
] )

The following operation uses the $match pipeline stage to find a specific document and the $replaceWith pipeline stage and the $setField operator to update the "price.usd" field in the matching document:

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 }
] )

The following operation uses the $match pipeline stage to find a specific document and the $replaceWith pipeline stage and the $setField and $literal operators to update the "$price" field in the matching document:

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 }
] )

The following operation uses the $replaceWith pipeline stage and the $setField operator and $$REMOVE to remove the "price.usd" field from each document:

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 }
] )

The following operation uses the $replaceWith pipeline stage, the $setField and $literal operators, and $$REMOVE to remove the "$price" field from each document:

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"
} } }
] )
이 페이지 평가하기

이 페이지의 내용