MongoDB - Update objects remove item in send array in object (nested updating) - node.js

this is my mongodb object
{
"_id" : ObjectId("4faaba123412d654fe83hg876"),
"user_id" : 123456,
"posters" : [ 123456,1111,456789,3333]
}
i want to add an item to posters array , or remove one how could i do it ?
its nested updating.
i saw some question around stackoverflow , but i didnt found any answer how to remove object from the array , lets say 3333 there .
so the result will be :
{
"_id" : ObjectId("4faaba123412d654fe83hg876"),
"user_id" : 123456,
"posters" : [ 123456,1111,456789]
}

Use $pull.
db.collection.update(
{ posters: "3333" },
{ $pull: { posters: "3333" } },
{ multi: true }
)

Use $push to add something into an array :
db.collection.update(
{ $push:
{
"posters" : "0000"
}
}
)
Use $pull to remove something from an array:
db.collection.update(
{ $pull:
{
"posters" : "3333"
}
}, True
)

Related

how to remove nested array value and get all values in a collection in mongoose?

I need to delete a record in a nested array in a collection
mongodb collection is having the following records
{
"_id" : ObjectId("589b043abc2f5a467c13303b"),
"user_id" : ObjectId("5874c174c813822341cb59a7"),
"filename" : [
{
"url" : "images/product_images/file-1486554170465.jpeg",
"_id" : ObjectId("589b043abc2f5a467c13303c")
},
{
"url" : "images/product_images/file-1486554306440.jpeg",
"_id" : ObjectId("589b04c2bc2f5a467c13303f")
}]
}
In that record i need to delete the fist url , which is under in filename array ,then i need the remaining url values , is there any way to implement this , kindly please help me . Thanks ...
You can use the $pull operator. In your case:
Collection
.update({
"_id": ObjectId("589b043abc2f5a467c13303b")
}, {
"$pull": {
"items": {
"_id": ObjectId("589b043abc2f5a467c13303c")
}
}
});

Removing An Object From An Array ($pull doesn't seem to work)

I'm trying to remove a lesson based on lesson_id from the following.
{
"_id" : ObjectId("5807f3f"),
"title" : "Title",
"lessons" : [
{
"lesson_id" : ObjectId("58073"),
"_id" : ObjectId("58074")
},
{
"lesson_id" : ObjectId("5807f5"),
"_id" : ObjectId("5807f6")
},
{
"lesson_id" : ObjectId("58077"),
"_id" : ObjectId("5807f4")
}
],
"__v" : 0
}
I've tried $pull and $unset, but with both my code seems to just set lessons.lesson_id to null and keep the lessons._id
Is there anyway to remove both from the object?
module.exports.deleteLessonFromClass = function(id, lesson_id, callback){ Class.update(
{'_id': id},
{ $unset: {lessons: {lesson_id: lesson_id}}},
callback
) }
Try the following query:
db.collection.update(
{ "_id" : <id> },
{ $pull : { "lessons" : { "lesson_id" : <lesson_id> } } }
);
This will find a document by <id> and remove its <lesson_id> from its lessons array.
Hope this helps.

How to update array with sub dictionary in Mongodb

I want to update a sub dictionary in an array.
my array like this
"comments" : [
{
"text" : "hi",
"_id" : ObjectId("56c552dd0a0f08b502a56521"),
"author" : {
"id" : ObjectId("56c54c73f96c51370294f254"),
"photo" : "",
"name" : ""
}
},
{
"text" : "Good",
"_id" : ObjectId("56c5911fc33b446c05a4dabc"),
"author" : {
"id" : ObjectId("56c54be6f96c51370294f123"),
"name" : "xxxx",
"photo":null
}
}
]
I want to update name and photo for this Id author.id:"56c54be6f96c51370294f123".
I wrote the code like this:
Event.find({'comments.author.id':_id},(err,event)=>{
_.each(event,function(eventData){
_.each(eventData.comments,function(commentData){
if(commentData.author.id == _id){
var data={'name':username,'photo':photoUrl};
Event.update({'commentData.author.id':_id},
{"$set":{"eventData.comments.author":data}},(err,commentupdate)=>{
console.log("commentupdate:"+JSON.stringify(commentupdate))
})
}
})
})
})
But I am unable to update the data please give me any suggestions. Thanks
You can do the update using the $ positional operator and applying the $set operator. The $ positional operator will identify the correct element in the array to update without explicitly specifying its position, thus your final update statement should look like:
Event.update(
{ "comments.author.id": _id },
{ "$set": {
"comments.$.author.name": username,
"comments.$.author.photo": photoUrl
}
},
(err, commentupdate)=>{
console.log("commentupdate:" + JSON.stringify(commentupdate))
}
)

Remove entry from double nester array using mongoose

I'm trying to remove an entry from an array which nested in another array as you can see below:
{
"_id" : ObjectId("548f5ca9fa9dc1000016a725"),
"entries" : [
{
"_id" : ObjectId("548f5cc8fa9dc1000016a726"),
"content" : [
{
"order" : ObjectId("5489fa9127f1310000bea2ed"),
"order_id" : "305429245",
"item_id" : "305429245-1"
},
{
"order" : ObjectId("5489fa9127f1310000bea2ce"),
"order_id" : "330052901",
"item_id" : "330052901-1"
}
],
"stop_number" : 1
},
{
"stop_number" : 2,
"expected_arrival" : ISODate("2014-12-15T17:11:11.000Z"),
"expected_departure" : ISODate("2014-12-15T19:03:17.000Z"),
"_id" : ObjectId("548fb2826e52c20000bd2299"),
"content" : []
}
]
}
And i'm trying to remove the entry that have '305429245-1', so i used:
Q.npost(Manifests, 'findOneAndUpdate', [
{ '_id': id },
{
'$pull': {
'entries.content': { item_id: line_item_id }
}
}
])
where 'id' is the ObjectID (548f5ca9fa9dc1000016a725) and line_item_id = 305429245-1, however, this doesn't work. Can anyone let me know what am i doing wrong?
Try to use find and Update functions separately instead of findOneAndUpdate
Model.find({'_id: id'},function(err,callback){...})
{
//handle callback
}
Model.update({'_id: id'}, {$pull: {'entries.content': item_id: line_item_id}}, function(err,callback){..} )
{
//do some logic or handle callback
}
here Model which i mentioned,should be the model you are using.

How to update a nested array in Node.js + Mongo DB where index value is known

I want to update nested array in Mongo DB (using Node.js). I am able to get the array index location.
How can I update same ? I am facing some problem while using escape character in $set
Here is what I am doing :
testCollection.update({
"uniqueID": someID,
"topLevelObject.innerObjectValue": innerObjectVal
}, {
$set: {
'array.' + outerArrayIndex + '.value': updatedValue,
}
}, function(err, result) {
if (err) {
console.log("Error occurred while updating db info");
}
}
);
It's hard to tell what the problem is because you have not included an example document or shown an error message or what goes wrong. Assuming your document looks like
{
"_id" : 0,
"array" : [{ "value" : 2 }, { "value" : 6 }]
}
then your above query should work, e.g.
db.test.update({ "_id" : 0 }, { "$set" : { "array.1.value" : 906 } })
will modify the document to
{
"_id" : 0,
"array" : [{ "value" : 2 }, { "value" : 906 }]
}
I omitted the other query condition because it isn't necessary with a unique id specified - perhaps the condition isn't matching any documents?
If you document looks like
{
"_id" : 0,
"array" : [2, 6]
}
then you don't need the .value in the update query:
db.test.update({ "_id" : 0 }, { "$set" : { "array.1" : 906 } })
I'd also check that the string concatenation with the variable outerArrayIndex is producing the correct field path.

Resources