 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pull value from array of ObjectIDs in MongoDB?
To pull value from array of ObjectIDs, use $pull in MongoDB. Let us create a collection with documents −
> db.demo258.insertOne({"arrayOfObjectsId":[ ObjectId("5e47a5e81627c0c63e7dba92"),ObjectId("5e47a5e51627c0c63e7dba91")]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a8211627c0c63e7dba97") } Display all documents from a collection with the help of find() method −
> db.demo258.find();
This will produce the following output −
{ "_id" : ObjectId("5e47a8211627c0c63e7dba97"), "arrayOfObjectsId" : [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ] } Following is the query to pull value from array of ObjectIDs −
> db.demo258.update( { }, { $pull: { arrayOfObjectsId: { $in: [ ObjectId("5e47a5e81627c0c63e7dba92") ] } } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) Display all documents from a collection with the help of find() method −
> db.demo258.find();
This will produce the following output −
{ "_id" : ObjectId("5e47a8211627c0c63e7dba97"), "arrayOfObjectsId" : [ ObjectId("5e47a5e51627c0c63e7dba91") ] }Advertisements
 