简体   繁体   English

使用猫鼬更新对象中数组内的值

[英]Update a value inside array in an object using mongoose

Hi I want to change a status as 'N' for the images which I have selected 嗨,我想将我选择的图像的状态更改为“ N”

Following is my json: 以下是我的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. 在media_id中,具有ID数组的ID只能更改状态。

My issue is value is updating but for the first value in array not for all 我的问题是值正在更新,但不是数组中的第一个值

This sounds like you'd want to use arrayFilters : 听起来您想使用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");  
    }
});

MongoDB has introduced the filtered positional operator $[] which can be used to update multiple elements of an array which match an array filter condition. MongoDB引入了过滤后的位置运算符$ [],可用于更新匹配数组过滤条件的数组的多个元素。 You can read more about this operator here: 您可以在此处阅读有关此运算符的更多信息:

https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/ 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);
       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM