Home > Database > MongoDB > body text

How to delete array elements by index in MongoDB?

PHPz
Release: 2023-09-08 12:17:02
forward
927 people have browsed it

如何在 MongoDB 中通过索引删除数组元素?

You can delete array elements by index using the following two steps -

The first step is as follows -

db.yourCollectionName.update({}, {$unset : {"yourArrayFieldName.yourIndexValue" : 1 }});
Copy after login

The above syntax will be in "yourIndexValue" Place a null value at the position. After that you need to extract null values ​​from array field to remove from array elements.

The second step is as follows -

db.yourCollectionName.update({}, {$pull : {"yourArrayFieldName" : null}});
Copy after login

To implement the syntax, let us create a collection with documents. The query to create a collection using documents is as follows -

> db.removeArrayElementByItsIndexDemo.insertOne({"InstructorName":"David",
   "InstructorAge":28,"InstructorSubject":["MongoDB","MySQL","Java","SQL Server","PL/SQL"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c8abbfc6cea1f28b7aa0803")
}
Copy after login

Display all the documents in the collection with the help of find() method. The query is as follows -

> db.removeArrayElementByItsIndexDemo.find().pretty();
Copy after login
Copy after login

The following is the output -

{
   "_id" : ObjectId("5c8abbfc6cea1f28b7aa0803"),
   "InstructorName" : "David",
   "InstructorAge" : 28,
   "InstructorSubject" : [
      "MongoDB",
      "MySQL",
      "Java",
      "SQL Server",
      "PL/SQL"
   ]
}
Copy after login

This is the query to delete array elements by index.

Step 1- Query as follows-

> db.removeArrayElementByItsIndexDemo.update({}, {$unset : {"InstructorSubject.2" : 1 }});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Copy after login

Step 2-Query as follows-

> db.removeArrayElementByItsIndexDemo.update({}, {$pull : {"InstructorSubject" : null}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Copy after login

Let’s check Whether the array element "Java" has been deleted. The query is as follows -

> db.removeArrayElementByItsIndexDemo.find().pretty();
Copy after login
Copy after login

The following is the output -

{
   "_id" : ObjectId("5c8abbfc6cea1f28b7aa0803"),
   "InstructorName" : "David",
   "InstructorAge" : 28,
   "InstructorSubject" : [
      "MongoDB",
      "MySQL",
      "SQL Server",
      "PL/SQL"
   ]
}
Copy after login

View the sample output, the array element "Java" has been completely deleted.

The above is the detailed content of How to delete array elements by index in MongoDB?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!