2

https://mongoplayground.net/p/Yv6vkGyrUS-

https://mongoplayground.net/p/vm314GoexjE

[
 {
   "_id": ObjectId("4b253b067525f35f94b60a31"),
  "age": 30,
  "favorite book": [
  "Cat's Cradle",
  "Foundation Trilogy",
  "Ender's Game"
   ],
"location": "Wisconsin",
"name": "joe",
"sex": "male"
}
]

output delete any element with based on condition

"favorite book": [
  "Cat's Cradle",
  "Foundation Trilogy"
   ]
3
  • 1
    Look at stackoverflow.com/questions/13423873/… Commented Dec 16, 2021 at 6:32
  • but i want del only array of obj a=[1,2,3] del a[1] now a=[1,3]... favbook=[1,2,3] del favbook[0] , favbook=[2,3] etc.. Commented Dec 16, 2021 at 6:40
  • $pull: { "favorite book": ?? } Commented Dec 16, 2021 at 6:42

1 Answer 1

1

You can use $pull operator

db.collection.update({
    { _id : id },
    { $pull: { "favorite book": "Ender's Game" } }
});

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

db.collection.update({
    { _id : id },
    { $pull: { "favorite book" : { $in : [ "Foundation Trilogy", "Ender's Game"] } } }
});

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

Sign up to request clarification or add additional context in comments.

2 Comments

its possible with unset ?
Yes. I think. But You will use the index of the element. But, it doesn’t remove the array element. Instead, it sets it to null. Check out mongodbtutorial.org/mongodb-crud/mongodb-remove-field-unset

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.