Definición
$unsetFieldNuevo en la versión 5.0.
Elimina un campo específico en un documento.
Puede usar
$unsetFieldpara remover campos con nombres que contengan puntos (.) o que comiencen con signos de dólar ($).$unsetFieldes un alias de$setFieldusando$$REMOVEpara remover campos.
Sintaxis
$unsetField tiene la siguiente sintaxis:
{ $unsetField: { field: <String>, input: <Object>, } }
Debes proporcionar los siguientes campos:
Campo | Tipo | Descripción |
|---|---|---|
| String | Campo en el |
| Objeto | Documento que contiene el |
Comportamiento
Si
inputse evalúa comomissing,undefinedonull,$unsetFieldretornanully no actualizainput.Si
inputevalúa cualquier cosa que no sea un objeto,missing,undefinedonull,$unsetFielddevuelve un error.Si
fieldresuelve cualquier cosa que no sea una constante de string,$unsetFielddevuelve un error.$unsetFieldno recorre implícitamente objetos o arreglos. Por ejemplo,$unsetFieldevalúa un valorfieldde"a.b.c"como un campo de nivel superior"a.b.c"en lugar de como un campo anidado,{ "a": { "b": { "c": } } }.
Ejemplos
Remover campos que contengan puntos (.)
Considera la colección de inventario:
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 } ] )
Utilice la etapa de pipeline $replaceWith y el operador $unsetField para remover el campo "price.usd" de cada documento:
db.inventory.aggregate( [ { $replaceWith: { $unsetField: { field: "price.usd", input: "$$ROOT" } } } ] )
La operación devuelve los siguientes 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 comienzan con signo de dólar ($)
Considera la colección de inventario:
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 } ] )
Utilice la $replaceWith etapa de canalización con los $unsetField operadores y para eliminar el $literal "$price" campo de cada documento:
db.inventory.aggregate( [ { $replaceWith: { $unsetField: { field: { $literal: "$price" }, input: "$$ROOT" } } } ] )
La operación devuelve los siguientes 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 un subcampo
Considera la colección de inventario:
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 } } ] )
El campo "price" contiene un documento con dos subcampos, "usd" y "euro". No puedes usar "price.euro" para identificar y remover "euro" porque MongoDB analiza "price.euro" como un nombre de campo de nivel superior que contiene un punto (.).
Utilice la etapa de pipeline $replaceWith con $setField y una operación anidada $unsetField para remover el campo "euro":
db.inventory.aggregate( [ { $replaceWith: { $setField: { field: "price", input: "$$ROOT", value: { $unsetField: { field: "euro", input: { $getField: "price" } } } } } } ] )
La operación devuelve los siguientes 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 } } ]