Tutorialspoint

1 Answer
Nishtha Thakur

You can use pop() for this. Let us first create a collection with documents −

> db.persistChangeDemo.insertOne({"Name" : "Larry", "CreditScore": [500,700,760,100]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cdfc52cbf3115999ed51203")
}

Following is the query to display all documents from a collection with the help of find() method −

> db.persistChangeDemo.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5cdfc52cbf3115999ed51203"),
   "Name" : "Larry",
   "CreditScore" : [
      500,
      700,
      760,
      100
   ]
}

Following is the query to pop a value −

> myDocument.CreditScore.pop();
100

Let us save the above document −

> db.persistChangeDemo.save(myDocument);
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Let us check the document once again −

> db.persistChangeDemo.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5cdfc52cbf3115999ed51203"),
   "Name" : "Larry",
   "CreditScore" : [
      500,
      700,
      760
   ]
}

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.