Definición
$unsetFieldNuevo en la versión 5.0.
Elimina un campo especificado en un documento.
Puede utilizar para eliminar campos con nombres que contengan puntos
$unsetField().o que comiencen con signos de dólar$().$unsetFieldes un alias para$setFieldusando$$REMOVEpara eliminar 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 se
inputevalúamissingundefinedcomo,nullo,$unsetFielddevuelvenully noinputactualiza.Si se
inputevalúa como algo distinto de unmissingobjeto,,undefinednullo, devuelve un$unsetFielderror.Si
fieldse resuelve en algo distinto a una constante de cadena, devuelve un$unsetFielderror.$unsetFieldno recorre implícitamente objetos ni matrices. Por ejemplo, evalúa$unsetFieldunfieldvalor de"a.b.c"como un campo de nivel superior"a.b.c"en lugar de como un campo{ "a": { "b": { "c": } } }anidado.
Ejemplos
Eliminar campos que contienen puntos. ()
Consideremos 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 $replaceWith etapa de canalización y el operador para eliminar $unsetField el "price.usd" campo 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 } ]
Eliminar campos que comienzan con un signo de dólar$ ()
Consideremos 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 } ]
Eliminar un subcampo
Consideremos 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 canalización$replaceWithcon$setFieldy una operación anidada$unsetFieldpara eliminar 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 } } ]