How to pull all elements from an array in MongoDB without any condition?

MongoDBDatabaseBig Data Analytics

You can use $set operator for this. Let us first create a collection with documents −

> db.pullAllElementDemo.insertOne(
...    {
...       "StudentId":101,
...       "StudentDetails" : [
...          {
...
...             "StudentName": "Carol",
...             "StudentAge":21,
...             "StudentCountryName":"US"
...          },
...          {
...             "StudentName": "Chris",
...             "StudentAge":24,
...             "StudentCountryName":"AUS"
...          }
...       ]
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccdd9c8685b30d09a7111e4")
}
> db.pullAllElementDemo.insertOne(
...    {
...       "StudentId":102,
...       "StudentDetails" : [
...          {
...
...             "StudentName": "Robert",
...             "StudentAge":27,
...             "StudentCountryName":"UK"
...          },
...          {
...             "StudentName": "David",
...             "StudentAge":23,
...             "StudentCountryName":"US"
...          }
...       ]
...    }
... );
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccdd9f7685b30d09a7111e5")
}

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

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

This will produce the following output −

{
   "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"),
   "StudentId" : 101,
   "StudentDetails" : [
      {
         "StudentName" : "Carol",
         "StudentAge" : 21,
         "StudentCountryName" : "US"
      },
      {
         "StudentName" : "Chris",
         "StudentAge" : 24,
         "StudentCountryName" : "AUS"
      }
   ]
}
{
   "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"),
   "StudentId" : 102,
   "StudentDetails" : [
      {
         "StudentName" : "Robert",
         "StudentAge" : 27,
         "StudentCountryName" : "UK"
      },
      {
         "StudentName" : "David",
         "StudentAge" : 23,
         "StudentCountryName" : "US"
      }
   ]
}

Following is the query to pull all elements from array in MongoDB without any condition. Here, we have removed StudentDetails with StudentId 102 using $set −

> db.pullAllElementDemo.update( {StudentId:102}, { "$set": { "StudentDetails": [] }} );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Let is display all documents from the above collection to check those specific elements from an array have been pulled out or not −

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

This will produce the following output −

{
   "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"),
   "StudentId" : 101,
   "StudentDetails" : [
      {
         "StudentName" : "Carol",
         "StudentAge" : 21,
         "StudentCountryName" : "US"
      },
      {
         "StudentName" : "Chris",
         "StudentAge" : 24,
         "StudentCountryName" : "AUS"
      }
   ]
}
{
   "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"),
   "StudentId" : 102,
   "StudentDetails" : [ ]
}
raja
Published on 21-May-2019 12:38:39
Advertisements