MongoDB, remove object from array
try..
db.mycollection.update(
{ '_id': ObjectId("5150a1199fac0e6910000002") },
{ $pull: { items: { id: 23 } } },
false, // Upsert
true, // Multi
);
Remove object within array
Use $pull operator to remove all matching objects.
MyCollection.update({_id: userId}, {
"$pull": {
"profiles.names": {
"first": "Joe"
}
}
});
To remove single object using $unset.When you use positional $ operator with update operation the array field must appear as part of the query document check docs
MyCollection.update({_id: userId,"profiles.names.first":"Joe"},{
"$unset":{
"profiles.names.$":""
}
});
Important Note : $unset
replaces the matching element with null rather than removing the matching element from the array. This behavior keeps consistent the array size and element positions.
So the execution of above query will update your names array and it will look like:
"profiles": {
"names": [
null,
{
"first": "Sam",
"last": "Last",
"uniqueId": "1488096533"
}
]
}
Removing of object completely is not yet supported by monngodb, see the open bug:
https://jira.mongodb.org/browse/SERVER-1014
Removing an element in a mongoDB array based on the position of element with dynamically defined array
On Mongo version 4.2+ You can use pipelined updates to achieve this, you can get it done in multiple ways, here is what I consider the easiest two ways:
- using
$slice
and$concatArrays
to remove a certain element:
db.collection.update({},
[
{
$set: {
[arrayName]: {
$concatArrays: [
{
$slice: [
`$${arrayName}`,
index,
]
},
{
$slice: [
`$${arrayName}`,
index + 1,
{
$size: `$${arrayName}`
}
]
}
]
}
}
}
])
Mongo Playground
- using
$filter
and$zip
to filter out based on index:
db.collection.updateOne(
{},
[
{
"$set": {
[arrayName]: {
$map: {
input: {
$filter: {
input: {
$zip: {
inputs: [
{
$range: [
0,
{
$size: `$${arrayName}`
}
]
},
`$${arrayName}`
]
}
},
cond: {
$ne: [
{
"$arrayElemAt": [
"$$this",
0
]
},
index
]
}
}
},
in: {
$arrayElemAt: [
"$$this",
1
]
}
}
}
}
}
])
Alternatively you can just prepare
How to remove an element from an array property in mongo collection
$pull
should do the trick:
db.groups.update(
{},
{ $pull: { members: "product:naxi" } },
{ multi: true }
)
How to remove array element in 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.
MongoDB: Remove attribute from nested Object Array
You want to use arrayfilters to do so:
db.collection.update({},
{
"$unset": {
"Blocks.$[block].Periods.$[period].IndividualAge": ""
}
},
{
"multi": false,
arrayFilters: [
{
"block.Name": "HouseKeepingDamageCost"
},
{
"period.Type": "Past"
}
]
})
Mongo Playground
Mongodb remove object from array
I think this might help
userModel.findOneAndUpdate({ email: email }, { $pull:{stock:{Ticker : tempStock}}}, (documents, err) => {
if (err) {
res.send(err);
}
else {
res.send(documents)
}
})
Also you might want to double-check
if you are receiving value in tempStock
variable
Remove Object from Array MongoDB
If I understand the requirements correctly, this should do the job:
House.update(
{ "expensesHouse._id": id },
{
$pull: {
expensesHouse: {
_id: id
}
}
}
)
Related Topics
Window.Onbeforeunload and Window.Onunload Is Not Working in Firefox, Safari, Opera
When Does Reflow Happen in a Dom Environment
How to Use Jquery for Xml Parsing with Namespaces
Adding Click Event Listener to Elements with the Same Class
Code Within D3.JSON() Callback Is Not Executed
Nested Routes with React Router V4/V5
What Is Typescript and Why Would I Use It in Place of JavaScript
Programmatically Lighten or Darken a Hex Color (Or Rgb, and Blend Colors)
Differencebetween Screenx/Y, Clientx/Y and Pagex/Y
When to Use Es6 Class Based React Components VS. Functional Es6 React Components
Find Out Whether Chrome Console Is Open
Accessing Jpeg Exif Rotation Data in JavaScript on the Client Side
Pass Props in Link React-Router
What's the Difference Between Window.Location and Document.Location in JavaScript
Check Image Width and Height Before Upload with JavaScript
Why Avoid Increment ("++") and Decrement ("--") Operators in JavaScript