정의
$setField버전 5.0에 추가.
문서에서 지정된 필드를 추가, 업데이트 또는 제거합니다.
$setField를 사용하여 마침표(.)가 포함되거나 달러 기호($)로 시작하는 이름의 필드를 추가, 업데이트 또는 제거 수 있습니다.
구문
$setField 의 구문은 다음과 같습니다:
{ $setField: { field: <String>, input: <Object>, value: <Expression> } }
다음 필드를 제공해야 합니다.
행동
input이missing,undefined또는null로 평가되면$setField는null을 반환하며,input을 업데이트하지 않습니다.input이 객체,missing,undefined또는null이외의 것으로 평가되면$setField는 오류를 반환합니다.field가 문자열 상수가 아닌 것으로 확인되면$setField는 오류를 반환합니다.field가input에 없으면$setField가 이를 추가합니다.$setField는 객체 또는 배열을 암시적으로 순회하지 않습니다. 예를 들어,$setField는"a.b.c"의field값을 중첩된 필드{ "a": { "b": { "c": } } }대신 최상위 필드"a.b.c"로 평가합니다.$unsetField은(는) 입력 값이$$REMOVE인$setField의 별칭입니다. 다음 표현식은 이와 동일합니다.{ $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" } } } ] )