Para agentes de IA: um índice de documentação está disponível em https://www.mongodb.com/pt-br/docs/llms.txt — as versões de markdown de todas as páginas estão disponíveis anexando .md a qualquer caminho de URL.
Menu Docs

$unset (estágio de agregação )

Observação

Desambiguação

The following page refers to the aggregation stage $unset. For the update operator $unset, see $unset.

$unset

Remove/exclui campos dos documentos.

The $unset stage has the following syntax:

  • To remove a single field, the $unset takes a string that specifies the field to remove:

    { $unset: "<field>" }
  • To remove multiple fields, the $unset takes an array of fields to remove.

    { $unset: [ "<field1>", "<field2>", ... ] }

The $unset is an alias for the $project stage that removes/excludes fields:

{ $project: { "<field1>": 0, "<field2>": 0, ... } }

Para remover/excluir um campo ou campos em um documento incorporado, você pode usar a notação de ponto, como em:

{ $unset: "<field.nestedfield>" }

ou

{ $unset: [ "<field1.nestedfield>", ...] }

Criar uma coleção books de amostra com os seguintes documentos:

db.books.insertMany([
{ "_id" : 1, title: "Antelope Antics", isbn: "0001122223334", author: { last:"An", first: "Auntie" }, copies: [ { warehouse: "A", qty: 5 }, { warehouse: "B", qty: 15 } ] },
{ "_id" : 2, title: "Bees Babble", isbn: "999999999333", author: { last:"Bumble", first: "Bee" }, copies: [ { warehouse: "A", qty: 2 }, { warehouse: "B", qty: 5 } ] }
])

O exemplo a seguir remove o campo de nível superior copies:

db.books.aggregate([ { $unset: "copies" } ])

Alternativamente, você também pode usar a seguinte sintaxe:

db.books.aggregate([ { $unset: [ "copies" ] } ])

Qualquer operação retorna os seguintes documentos:

{ "_id" : 1, "title" : "Antelope Antics", "isbn" : "0001122223334", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "isbn" : "999999999333", "author" : { "last" : "Bumble", "first" : "Bee" } }

O exemplo a seguir remove os campos de nível superior isbn e copies:

db.books.aggregate([
{ $unset: [ "isbn", "copies" ] }
])

The $unset operation outputs the following documents:

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An", "first" : "Auntie" } }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble", "first" : "Bee" } }

O exemplo a seguir remove o campo de nível superior isbn, o campo incorporado first (do documento name) e o campo incorporado warehouse (dos elementos na array copies):

db.books.aggregate([
{ $unset: [ "isbn", "author.first", "copies.warehouse" ] }
])

The $unset operation outputs the following documents:

{ "_id" : 1, "title" : "Antelope Antics", "author" : { "last" : "An" }, "copies" : [ { "qty" : 5 }, { "qty" : 15 } ] }
{ "_id" : 2, "title" : "Bees Babble", "author" : { "last" : "Bumble" }, "copies" : [ { "qty" : 2 }, { "qty" : 5 } ] }