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))
}
)
Related
I have a dataset like this:
{
"_id" : ObjectId("5a7bee68996b551034015a15"),
"sequenceid" : 1,
"fruit" : [
{
"name" : "#APPLE",
"value" : 2
},
{
"name" : "#BANANA",
"value" : 1
},
{
"name" : "#ORANGE",
"value" : 5
}
}
want to update only Apple value i.e from 2 to 25. Expected result will be:
{
"_id" : ObjectId("5a7bee68996b551034015a15"),
"sequenceid" : 1,
"fruit" : [
{
"name" : "#APPLE",
"value" : 25
},
{
"name" : "#BANANA",
"value" : 1
},
{
"name" : "#ORANGE",
"value" : 5
}
}
I tried the code but this will replace all entry and do only one entry. My code is
db.Collection.update({'sequenceid': 1}, {$set: {'fruit' : {'name': '#APPLE', 'value': parseFloat(25)}}}, function(error, result){
if(error){
console.log('error');
} else {
console.log('success');
}
});
It can produce the result:
{
"_id" : ObjectId("5a7bee68996b551034015a15"),
"sequenceid" : 1,
"fruit" : [
{
"name" : "#APPLE",
"value" : 25
}
}//Delete all my rest entry
How I can Do this. I am a newbie on MongoDB
This will update only the first occurrence of record.For reference MongoDB - Update objects in a document's array (nested updating)
db.collection.update({ _id: ObjectId("5a7bf5586262dc7b9f3a8422") ,"fruit.name" : "#APPLE"},
{ $set:
{
"fruit.$.value" : 25
}
})
If you are writing JavaScript query then you can update like this
db.collection.find({'sequenceid': 1}).forEach(function(x){
x.fruit.forEach(function(y){
if(y.name=="#APPLE")
{
y.value = 25
}
})
db.collection.update({_id:x._id},x)
})
db.Collection.update({
_id: ObjectId("5a7bee68996b551034015a15"),
"fruit": {
$elemMatch: {
"name": "#APPLE"
}
}
}, {
$set: {
"fruit.$.value": 25
}
})
In above update operation $elemMatch operator is used to search a value in an array and in $set stage positional operator $ is used to update value of specific key belonging to an array element
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")
}
}
});
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.
I am new to node.js.
I am having JSON object of the form
{ "_id" : ObjectId("540b03ddf1768fe562fbb715"),
"productid" : "men1",
"comments" : [ { "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540b03dd0570a6261e20a59e"), "date_entered" : ISODate("2014-09-06T12:53:49.658Z") },
{ "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540cb2be35f8145a2d296ea0"), "date_entered" : ISODate("2014-09-07T19:32:14.827Z") },
{ "name" : "shiva", "text" : "Haidddsdcccccc", "_id" : ObjectId("540cb2c335f8145a2d296ea1"), "date_entered" : ISODate("2014-09-07T19:32:19.456Z") } ] }
I want to query comments of a product after a specific time
I am using query as
var query=Product.find({"productid":obj.productid,
'comments.date_entered': {$gt: obj.timed}}, {'comments.$': 1})
I am getting only one object after the specific time i.e
{
_id: "540b03ddf1768fe562fbb715"
comments: [1]
0: {
name: "shiva"
text: "Haidddsdcccccc"
_id: "540cb2be35f8145a2d296ea0"
date_entered: "2014-09-07T19:32:14.827Z"
}
}
How to get all the comments of the product after the specified time?
Doesn't seem to be a good way to do this from what I can find out. A work-around could be doing something like this:
Product.findOne({productid: PRODUCT_ID}, {comments: 1}, function(err, product) {
var comments = product.comments.filter(function(comment) {
return comment.date_entered > DATE;
});
...
})
I have the following document:
{
"_id" : ObjectId("503b83dfad79cc8d26000004"),
"pdfs" : [
{
"title" : "Test document",
"pdf_id" : ObjectId("504f6793ce351a595d000004"),
"created_at" : ISODate("2012-09-11T16:32:19.276Z")
},
{
"title" : "Some other doc",
"pdf_id" : ObjectId("502bf124b4642341230003f0"),
"created_at" : ISODate("2012-09-11T11:34:19.276Z")
}
]
}
Now in an incoming form via req.body, I have 2 fields: title and description.
I want to update title and insert description for a specified pdf_id, how do I do that?
So in the end, my document will now look like:
{
"_id" : ObjectId("503b83dfad79cc8d26000004"),
"pdfs" : [
{
"title" : "This is an UPDATED title",
"description" : "It has an ALL NEW description",
"pdf_id" : ObjectId("504f6793ce351a595d000004"),
"created_at" : ISODate("2012-09-11T16:32:19.276Z")
},
{
"title" : "Some other doc",
"pdf_id" : ObjectId("502bf124b4642341230003f0"),
"created_at" : ISODate("2012-09-11T11:34:19.276Z")
}
]
}
Just to be clear, I'm really just looking for the Mongoose update syntax.
You can use the $ positional operator to refer to the matched pdfs array element in your $set:
Model.update(
{ 'pdfs.pdf_id': pdf_id },
{ $set: {
'pdfs.$.title': title,
'pdfs.$.description': description
}}, function (err, numAffected) { ... }
);