0

Hi I want to change a status as 'N' for the images which I have selected

Following is my json:

 {
 "_id" : ObjectId("5b9f95c2e7d46ca2bffb8a38"),
 "inspection_id" : "00000000dc174c30682f9bdd",
 "__v" : 0,
 "client_id" : "00000000d080d32e387121bf",
 "created_date" : ISODate("2018-09-17T12:44:01.843Z"),
 "images" : [ 
 {
 "img_name" : "1537185218432-dnIyQ1tL4di9jBmP.png",
 "type" : "png",
 "file_size" : "6985",
 "path" : "/uploads/inspection/00000000d080d32e387121bf/images/1537185218432-dnIyQ1tL4di9jBmP.png",
 "status" : "Y",
 "_id" : ObjectId("5b9f95c23c4a081a34b4cf1a")
 }, 
 {
 "img_name" : "1537185451994-FS2yNFcNKhASSHqe.jpg",
 "type" : "jpg",
 "file_size" : "41341",
 "path" : "/uploads/inspection/00000000d080d32e387121bf/images/1537185451994-FS2yNFcNKhASSHqe.jpg",
 "status" : "Y",
 "_id" : ObjectId("5b9f96ac5700fc07000ee07f")
 }, 
 {
 "img_name" : "1537186207337-hhz5XrpmH48YW47A.jpg",
 "type" : "jpg",
 "file_size" : "41341",
 "path" : "/uploads/inspection/00000000d080d32e387121bf/images/1537186207337-hhz5XrpmH48YW47A.jpg",
 "status" : "N",
 "_id" : ObjectId("5b9f999f5700fc07000ee081")
 }]
 }

I tried this following query:

mongo.inspectionmedia.update({'images._id' : {$in:media_id} }, { $set : { 'images.$.status' : 'N'} },{multi: true},function(err,output){
 if(err){
 console.log(err)
 res.json("err");
 }
 else{
 res.json("ok"); 
 }
 });

In media_id am having array of id for those id only status have to change.

My issue is value is updating but for the first value in array not for all

asked Sep 20, 2018 at 4:34

2 Answers 2

1

This sounds like you'd want to use arrayFilters:

db.inspectionmedia.update({}, {
 $set : { 'images.$[i].status' : 'N'} 
}, {
 multi: true,
 arrayFilters: [
 {'i._id': { $in:media_id}}
 ]
}, function(err,output){
 if(err){
 console.log(err)
 res.json("err");
 }
 else{
 res.json("ok"); 
 }
});
answered Sep 20, 2018 at 5:00
Sign up to request clarification or add additional context in comments.

Comments

1

MongoDB has introduced the filtered positional operator $[] which can be used to update multiple elements of an array which match an array filter condition. You can read more about this operator here:

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/

 db.inspectionmedia.update(
 {$set: { 'images.$[element].status' : 'N'} },
 {multi:true,
 arrayFilters: [
 {'element._id': { $in:media_id}}
 ]
 },
 function(err,updated){
 console.log(updated);
 });
answered Sep 20, 2018 at 5:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.