MongoDB array update operation
Update single array element
Demo data:
{
name:"codelearningpoint",
skills:[
{ name:"Nodejs" , key:"nodejs"},
{ name:"MongoDB" , key:"mongodb"}
]
}
1. I want to update exact { name:"Nodejs" , key:"nodejs"}
array element.
db.collection.updateOne(
{ "skills.key" : 'nodejs' },
{
$set : {"skills.$": { name:"React" , key : "react"} }
}
)
Output : $ refers to the exact matched first element.
{
name:"codelearningpoint",
skills:[
{ name:"React" , key:"react"},
{ name:"MongoDB" , key:"mongodb"}
]
}
2. I want to add active : true
property to all the skills , how to do that ?
db.collection.updateMany(
{ $or:[{key:"mongodb"},{key:"nodejs"}] },
{
$set : {"skills.$.active": true }
}
)
Output : It did not update all the documents
Reason $ sign only refers to the first match element. that is why it has updated only the first element.
Note : if filter is empty the we can not use $
{
name:"codelearningpoint",
skills:[
{ name:"React" , key:"react" , active : true},
{ name:"MongoDB" , key:"mongodb" }
]
}
Update All array element
In order to achieve that we need use $[] , $[] refers to all the element of the array
db.collection.updateMany(
{ $or:[{key:"mongodb"},{key:"nodejs"}] },
{
$set : {"skills.$[].active": true }
}
)
OR
db.collection.updateMany(
{},
{
$set : {"skills.$[].active": true }
}
)
Output:
{
name:"codelearningpoint",
skills:[
{ name:"React" , key:"react" , active : true},
{ name:"MongoDB" , key:"mongodb" , active : true}
]
}
Finding and updating specific fields (filterArrays)
Demo Data
{
key: 'brand',
displayName: 'Brand',
options: [
{
key: 'samsung',
displayName: 'Samsung',
count: 0,
selected: false,
active: true
},
{
key: 'lg',
displayName: 'LG',
count: 0,
selected: false,
active: true,
},
{
key: 'nokia',
displayName: 'Nokia',
count: 0,
selected: false,
active: true
},
{
key: 'apple',
displayName: 'Apple',
count: 0,
selected: false,
active: true
}
]
}
1. I want to add one new property {bestBrand : true}
to the options array element if the key matches.
db.collection.updateOne(
{
$or:[
{"options.key":"apple"},
{"options.key":"lg"}]
},
{$set:
{"options.$[el].bestBrand":true}
},
{
arrayFilters:[
{
$or:[{"el.key":"apple"},{"el.key":"lg"}]
}
]
}
)
Note : The main filter and the arrayFilter can be different.
el is used as an identifier used for the arrayFilters[] condition.
Update 3 layer nested array in mongoDB
{
"_id" : 1,
"grades" : [
{ "type" : "quiz1", "questions" : [ 1, 10, 5 ] },
{ "type" : "quiz2", "questions" : [ 10, 11, 6 ] },
{ "type" : "quiz3", "questions" : [ 5, 4, 3 ] },
{ "type" : "quiz4", "questions" : [ 2, 10, 23, 0 ] }
]
}
> Update all the grade elements
> if the questions value id greater than equal 6 then the value will be increased by 1
db.collection.updateMany(
{},
{ $inc: { "grades.$[].questions.$[score]": 1 } },
{ arrayFilters: [ { "score": { $gte: 6 } } ] }
)
> Increment all the questions value by 1 if the grade type is quiz2
db.collection.updateMany(
{},
{ $inc: { "grades.$[el].questions.$[score]": 1 } },
{ arrayFilters: [ { "el.type": "quiz" }, { "score": { $gte: 6 } } ] }
)
How to add elements to array in mongoDB
How to add single element to array ?
db.collection.updateOne(
{"skills.key":"nodejs"},
{$push : {"skills":{ name:"HTML" , key:"html" }}
}
)
How to add multiple element to array ?
db.collection.updateOne(
{"skills.key":"nodejs"},
{
$push : {
"skills":{ $each:[ { name:"HTML" , key:"html" },{ name:"CSS" , key:"css" } ] }
}
}
)
Sort array elements while adding to array.
db.collection.updateOne(
{"skills.key":"nodejs"},
{
$push : {
"skills":{ $each:[ { name:"HTML" , key:"html" },{ name:"CSS" , key:"css" } ] , $sort: { name: -1 } }
}
}
)
While adding i want to avoid pushing duplicate elements , for that i we will use $addToSet
db.collection.updateOne(
{"skills.key":"nodejs"},
{
$addToSet : { "skills":{ name:"HTML" , key:"html" }}
}
)
How to remove element from array in mongoDB
Demo data:
{
name:"codelearningpoint",
skills:[
{ name:"Nodejs" , key:"nodejs"},
{ name:"MongoDB" , key:"mongodb"}
]
}
1. I want to remove an element from skills array if the key is nodejs.
$pull helps to remove the element , in {$pull : { skills : {key : "nodejs"}}} { skills : {key : "nodejs"}}
is the condition or we can use any filter operators here.
{ $pull: { <field1>: <value||condition>, <field2>: <value||condition> } }
db.collection.updateOne(
{"skills.key" : "nodejs"},
{$pull : { skills : {key : "nodejs"}}}
)
Output :
{
name:"codelearningpoint",
skills:[
{ name:"MongoDB" , key:"mongodb"}
]
}
2. I want to remove the last element from skills array
db.collection.updateOne(
{"skills.key" : "nodejs"},
{$pop : { skills : 1 }}
)
Output :
{
name:"codelearningpoint",
skills:[
{ name:"Nodejs" , key:"nodejs"}
]
}
3. I want to remove first element from skills array
db.collection.updateOne(
{"skills.key" : "nodejs"},
{$pull : { skills : -1 }}
)
Output :
{
name:"codelearningpoint",
skills:[
{ name:"MongoDB" , key:"mongodb"}
]
}
Note : Skills : -1 , will remove the first element from skills array and skills:1 will remove the last element from skills array.
Multiple operations at same time
Demo data :
{
name:"codelearningpoint",
skills:[
{ name:"Nodejs" , key:"nodejs"},
{ name:"MongoDB" , key:"mongodb"}
]
}
Fins the document and add new test:true property and delete the nodejs object
db.collection.updateOne({"skills.key":'nodejs'}, {$set: {test:true}, $pull:{ skills :{ key: 'nodejs'} } } )
Output :
{
name:"codelearningpoint",
test:true,
skills:[
{ name:"MongoDB" , key:"mongodb"}
]
}