Definição
$unsetFieldNovidade na versão 5.0.
Remove um campo especificado em um documento.
Você pode utilizar para remover campos com nomes que contêm
$unsetFieldpontos.() ou que começam com cifrões de dólar$().$unsetFieldé um alias para que$setFieldusa$$REMOVEpara remover campos.
Sintaxe
$unsetField tem a seguinte sintaxe:
{ $unsetField: { field: <String>, input: <Object>, } }
Você deve fornecer os seguintes campos:
Campo | Tipo | Descrição |
|---|---|---|
| String | Campo no objeto |
| Objeto | Documento que contém o |
Comportamento
Se
inputavaliarmissingpara,undefinednullou,$unsetFieldgeranulle nãoinputatualiza.Se for
inputavaliado como algo diferente de um objeto,,missingundefinednullou, retornará um$unsetFielderro.Se
fieldresolver para algo diferente de uma constante de string, retornará um$unsetFielderro.$unsetFieldnão atravessa implicitamente objetos ou arrays. Por exemplo, avalia$unsetFieldumfieldvalor de"a.b.c"como um campo de nível superior"a.b.c"em vez de um campo aninhado,.{ "a": { "b": { "c": } } }
Exemplos
Remover campos que contêm períodos (.)
Considere a coleção de inventário:
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 } ] )
Use o $replaceWith estágio de pipeline e o operador para remover $unsetField o "price.usd" campo de cada documento:
db.inventory.aggregate( [ { $replaceWith: { $unsetField: { field: "price.usd", input: "$$ROOT" } } } ] )
A operação retorna os seguintes resultados:
[ { _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 } ]
Remover campos que começam com um cifrão ($)
Considere a coleção de inventário:
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 } ] )
Use o $replaceWith estágio de pipeline com os $unsetField operadores e para remover $literal o "$price" campo de cada documento:
db.inventory.aggregate( [ { $replaceWith: { $unsetField: { field: { $literal: "$price" }, input: "$$ROOT" } } } ] )
A operação retorna os seguintes resultados:
[ { _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 } ]
Remover um subcampo
Considere a coleção de inventário:
db.inventory.insertMany( [ { _id: 1, item: "sweatshirt", qty: 300, "price": {"usd":45.99, "euro": 38.77 } }, { _id: 2, item: "winter coat", qty: 200, "price": { "usd": 499.99, "euro": 420.51 } }, { _id: 3, item: "sun dress", qty: 250, "price": { "usd": 199.99, "euro": 167.70 } }, { _id: 4, item: "leather boots", qty: 300, "price": { "usd": 249.99, "euro": 210.68 } }, { _id: 5, item: "bow tie", qty: 180, "price": { "usd": 9.99, "euro": 8.42 } } ] )
O campo "price" contém um documento com dois subcampos, "usd" e "euro". Você não pode usar "price.euro" para identificar e remover "euro" porque o MongoDB analisa "price.euro" como um nome de campo de nível superior que por acaso contém um ponto (.).
Use o estágio do pipeline$replaceWithcom$setFielde uma operação$unsetFieldaninhada para remover o campo "euro":
db.inventory.aggregate( [ { $replaceWith: { $setField: { field: "price", input: "$$ROOT", value: { $unsetField: { field: "euro", input: { $getField: "price" } } } } } } ] )
A operação retorna os seguintes resultados:
[ { _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 } } ]