Para agentes de IA: hay un índice de documentación disponible en https://www.mongodb.com/es/docs/llms.txt — versiones en markdown de todas las páginas están disponibles agregando .md a cualquier ruta URL.
Docs Menu

$unsetField (operador de expresión)

$unsetField

Nuevo en la versión 5.0.

Elimina un campo específico en un documento.

Puedes usar $unsetField para eliminar campos cuyos nombres contengan puntos (.) o que comiencen con signos de dólar ($).

$unsetField es un alias de $setField que utiliza $$REMOVE para eliminar campos.

$unsetField tiene la siguiente sintaxis:

{
$unsetField: {
field: <String>,
input: <Object>,
}
}

Debes proporcionar los siguientes campos:

Campo
Tipo
Descripción

field

String

Campo en el objeto input que deseas remover. field puede ser cualquier expresión válida que dé como resultado una constante string.

input

Objeto

Documento que contiene el field que deseas remover. input debe resolverse en un objeto, missing, null o undefined.

  • Si input se evalúa como missing, undefined o null, $unsetField devuelve null y no actualiza input.

  • Si input se evalúa como algo distinto a un objeto, missing, undefined o null, $unsetField devuelve un error.

  • Si field se resuelve en algo distinto a una constante de cadena, $unsetField devuelve un error.

  • $unsetField no recorre implícitamente objetos o matrices. Por ejemplo, $unsetField evalúa un valor field de "a.b.c" como un campo de nivel superior "a.b.c" en lugar de como un campo anidado, { "a": { "b": { "c": } } }.

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 canalización $replaceWith y el operador $unsetField para eliminar 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 }
]

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 etapa de canalización $replaceWith con los operadores $unsetField y $literal para eliminar el campo "$price" 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 }
]

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 canalización $replaceWith con $setField y una operación $unsetField anidada para 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 } }
]

$setField