Mongodb, Remove Object from Array

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:

  1. 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


  1. 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



Leave a reply



Submit