How do I remove a string from an array in a mongodb document?

Ethan Wilberforce

I've been having trouble with the problem of removing a string from an array in a mongodb document. For example I want to remove the entry "four" from the list field in the document below.

{
  "list" : [ "1", "2", "four", "five", "6"]
}

I know it seems very simple but I haven't been able to find a straight answer from the docs or previous questions. I've tried 5 or so commands to no avail using db.collection.update combined with the $pull and $mod modifiers. Any help?

I know, very rudimentary question.

Yathish Manjunath

You can use $Pull operator, please try the below query :

db.collection.update(
                      { _id : id },
                      { $pull: 
                             { "list":"four" }
                      }
                     );

If you want to remove two or more elements from the arry "list", you can do that as well with $Pull operator as well :

db.collection.update( { _id : id },
                      { $pull: 
                              { list : 
                                      { $in : [ "one", "four" ] }
                              }
                       }
                     );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to delete or remove array data from document i n mongodb using php

From Dev

How to remove property from document embedded in array using MongoDB?

From Dev

How do I add an array of elements in MongoDB to an array in an existing document?

From Dev

How do I update an array from MongoDB document that match a certain value?

From Dev

Remove element of array from document in MongoDb

From Dev

How do I remove an array inside an array in mongoDB?

From Dev

How do I remove CSS from a string?

From Dev

How Do I Remove A Vowel From String

From Dev

How do i remove chars from a string?

From Dev

How do I remove punctuaution from a string

From Dev

how do i remove a letter from string

From Dev

How do I remove values from an array?

From Dev

How do I remove duplicates from an array

From Dev

How do I Remove all the namespaces from a dom Document

From Dev

How do I remove duplicate notes from an XML document in Perl?

From Dev

How do I remove the Header and footer "area" from a word document?

From Dev

How do I delete values from this document in MongoDB using Python

From Dev

How do I delete values from this document in MongoDB using Python

From Dev

How do I remove an array item in Mongodb / Golang?

From Dev

How do I remove commas from an array and make it a single string variable in JavaScript?

From Dev

How can i remove empty string from a mongodb collection?

From Dev

How do I remove empty cells in a string array?

From Dev

Remove item from array in a mongodb document using Doctrine ODM

From Dev

How do I remove a character from a string in a Unix shell script?

From Dev

How do I remove all occurrences of a specific char from a string?

From Dev

How do I remove multiple offending characters from my string?

From Dev

How do I remove \r\n from string in C#?

From Dev

How do I remove a character from a list or string in Erlang?

From Dev

How do I remove new line characters from a string?

Related Related

  1. 1

    How to delete or remove array data from document i n mongodb using php

  2. 2

    How to remove property from document embedded in array using MongoDB?

  3. 3

    How do I add an array of elements in MongoDB to an array in an existing document?

  4. 4

    How do I update an array from MongoDB document that match a certain value?

  5. 5

    Remove element of array from document in MongoDb

  6. 6

    How do I remove an array inside an array in mongoDB?

  7. 7

    How do I remove CSS from a string?

  8. 8

    How Do I Remove A Vowel From String

  9. 9

    How do i remove chars from a string?

  10. 10

    How do I remove punctuaution from a string

  11. 11

    how do i remove a letter from string

  12. 12

    How do I remove values from an array?

  13. 13

    How do I remove duplicates from an array

  14. 14

    How do I Remove all the namespaces from a dom Document

  15. 15

    How do I remove duplicate notes from an XML document in Perl?

  16. 16

    How do I remove the Header and footer "area" from a word document?

  17. 17

    How do I delete values from this document in MongoDB using Python

  18. 18

    How do I delete values from this document in MongoDB using Python

  19. 19

    How do I remove an array item in Mongodb / Golang?

  20. 20

    How do I remove commas from an array and make it a single string variable in JavaScript?

  21. 21

    How can i remove empty string from a mongodb collection?

  22. 22

    How do I remove empty cells in a string array?

  23. 23

    Remove item from array in a mongodb document using Doctrine ODM

  24. 24

    How do I remove a character from a string in a Unix shell script?

  25. 25

    How do I remove all occurrences of a specific char from a string?

  26. 26

    How do I remove multiple offending characters from my string?

  27. 27

    How do I remove \r\n from string in C#?

  28. 28

    How do I remove a character from a list or string in Erlang?

  29. 29

    How do I remove new line characters from a string?

HotTag

Archive