How to remove array element in mongodb?

Mongodb

Mongodb Problem Overview


Here is array structure

contact: {
	phone: [
	    {
	        number: "+1786543589455",
	        place: "New Jersey",
	        createdAt: ""
	    }
	    {
	        number: "+1986543589455",
	        place: "Houston",
	        createdAt: ""
	    }

	]
}

Here I only know the mongo id(_id) and phone number(+1786543589455) and I need to remove that whole corresponding array element from document. i.e zero indexed element in phone array is matched with phone number and need to remove the corresponding array element.

contact: {
	phone: [
	    {
	        number: "+1986543589455",
	        place: "Houston",
	        createdAt: ""
	    }
	]
}

I tried with following update method

collection.update(
    { _id: id, 'contact.phone': '+1786543589455' },
    { $unset: { 'contact.phone.$.number': '+1786543589455'} }
);

But it removes number: +1786543589455 from inner array object, not zero indexed element in phone array. Tried with pull also without a success.

How to remove the array element in mongodb?

Mongodb Solutions


Solution 1 - Mongodb

Try the following query:

collection.update(
  { _id: id },
  { $pull: { 'contact.phone': { number: '+1786543589455' } } }
);

It will find document with the given _id and remove the phone +1786543589455 from its contact.phone array.

You can use $unset to unset the value in the array (set it to null), but not to remove it completely.

Solution 2 - Mongodb

This below code will remove the complete object element from the array, where the phone number is '+1786543589455'

db.collection.update(
  { _id: id },
  { $pull: { 'contact': { number: '+1786543589455' } } }
);

Solution 3 - Mongodb

You can simply use $pull to remove a sub-document. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

Collection.update({
    _id: parentDocumentId
  }, {
    $pull: {
      subDocument: {
        _id: SubDocumentId
      }
    }
  });

This will find your parent document against given ID and then will remove the element from subDocument which matched the given criteria.

Read more about pull here.

Solution 4 - Mongodb

In Mongoose: from the document:

> To remove a document from a subdocument array we may pass an object > with a matching _id.

contact.phone.pull({ _id: itemId }) // remove
contact.phone.pull(itemId); // this also works

See Leonid Beschastny's answer for the correct answer.

Solution 5 - Mongodb

To remove all array elements irrespective of any given id, use this:

collection.update(
  { },
  { $pull: { 'contact.phone': { number: '+1786543589455' } } }
);

Solution 6 - Mongodb

Given the following document in the profiles collection:

{ _id: 1, votes: [ 3, 5, 6, 7, 7, 8 ] }

The following operation will remove all items from the votes array that are greater than or equal to ($gte) 6:

db.profiles.update( { _id: 1 }, { $pull: { votes: { $gte: 6 } } } )

After the update operation, the document only has values less than 6:

{ _id: 1, votes: [ 3, 5 ] }

If you multiple items the same value, you should use $pullAll instead of $pull.

In the question having a multiple contact numbers the same use this:

collection.update( { _id: id }, { $pullAll: { 'contact.phone': { number: '+1786543589455' } } } ); it will delete every item that matches that number. in contact phone

Try reading the manual.

Solution 7 - Mongodb

If you use the Mongoose API and looking to pull a sub/child object: Read this document Don't forget to use save() when you're done editing otherwise the changes won't be saved to the database.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJustin JohnView Question on Stackoverflow
Solution 1 - MongodbLeonid BeschastnyView Answer on Stackoverflow
Solution 2 - Mongodbchirag jainView Answer on Stackoverflow
Solution 3 - MongodbSyeda Aimen BatoolView Answer on Stackoverflow
Solution 4 - MongodbMd AlaminView Answer on Stackoverflow
Solution 5 - MongodbKayVView Answer on Stackoverflow
Solution 6 - MongodbDonald FrenchView Answer on Stackoverflow
Solution 7 - MongodbNonameLoverView Answer on Stackoverflow