Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/ /

$unsetField (operador de expresión)

$unsetField

Nuevo en la versión 5.0.

Elimina un campo específico en un documento.

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

$unsetField es un alias de $setField usando $$REMOVE para remover 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 retorna null y no actualiza input.

  • Si input evalúa cualquier cosa que no sea un objeto, missing, undefined o null, $unsetField devuelve un error.

  • Si field resuelve cualquier cosa que no sea una constante de string, $unsetField devuelve un error.

  • $unsetField no recorre implícitamente objetos o arreglos. 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 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 }
]

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 }
] )

Utiliza la etapa de pipeline $replaceWith con el $unsetField y los operadores $literal para remover 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 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 } }
]

$setField

Volver

$type

En esta página