

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# \$1pop
<a name="pop"></a>

L'`$pop`operatore in Amazon DocumentDB viene utilizzato per rimuovere il primo o l'ultimo elemento da un campo array. È particolarmente utile quando è necessario mantenere un array a dimensione fissa o implementare una struttura di dati simile a una coda all'interno di un documento.

**Parametri**
+ `field`: il nome del campo dell'array da cui rimuovere un elemento.
+ `value`: un valore intero che determina la posizione dell'elemento da rimuovere. Un valore di `1` rimuove l'ultimo elemento, mentre un valore di `-1` rimuove il primo elemento.

## Esempio (MongoDB Shell)
<a name="pop-examples"></a>

Questo esempio dimostra come utilizzare l'`$pop`operatore per rimuovere il primo e l'ultimo elemento da un campo di matrice.

**Crea documenti di esempio**

```
db.users.insertMany([
  { "_id": 1, "name": "John Doe", "hobbies": ["reading", "swimming", "hiking"] },
  { "_id": 2, "name": "Jane Smith", "hobbies": ["cooking", "gardening", "painting"] }
])
```

**Esempio di interrogazione**

```
// Remove the first element from the "hobbies" array
db.users.update({ "_id": 1 }, { $pop: { "hobbies": -1 } })

// Remove the last element from the "hobbies" array
db.users.update({ "_id": 2 }, { $pop: { "hobbies": 1 } })
```

**Output**

```
{ "_id" : 1, "name" : "John Doe", "hobbies" : [ "swimming", "hiking" ] }
{ "_id" : 2, "name" : "Jane Smith", "hobbies" : [ "cooking", "gardening" ] }
```

## Esempi di codice
<a name="pop-code"></a>

Per visualizzare un esempio di codice per l'utilizzo del `$pop` comando, scegliete la scheda relativa alla lingua che desiderate utilizzare:

------
#### [ Node.js ]

```
const { MongoClient } = require('mongodb');

async function example() {
  const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false');
  const db = client.db('test');
  const collection = db.collection('users');

  // Remove the first element from the "hobbies" array
  await collection.updateOne({ "_id": 1 }, { $pop: { "hobbies": -1 } });

  // Remove the last element from the "hobbies" array
  await collection.updateOne({ "_id": 2 }, { $pop: { "hobbies": 1 } });

  const users = await collection.find().toArray();
  console.log(users);

  await client.close();
}

example();
```

------
#### [ Python ]

```
from pymongo import MongoClient

def example():
    client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false')
    db = client['test']
    collection = db['users']

    # Remove the first element from the "hobbies" array
    collection.update_one({"_id": 1}, {"$pop": {"hobbies": -1}})

    # Remove the last element from the "hobbies" array
    collection.update_one({"_id": 2}, {"$pop": {"hobbies": 1}})

    users = list(collection.find())
    print(users)

    client.close()

example()
```

------