Remove sub-document from Mongo with mongoose

后端 未结 6 554
情歌与酒
情歌与酒 2020-12-16 01:12

I am trying to remove an item from a collection that is stored in a mongoose document. My document looks like this:

{
  \"__v\": 3,
  \"_id\": \"5221040475f         


        
6条回答
  •  忘掉有多难
    2020-12-16 01:44

    finaly!

    MongoDB:
    
    "imgs" : {"other" : [ {
            "crop" : "../uploads/584251f58148e3150fa5c1a7/photo_2016-11-09_21-38-55.jpg",
            "origin" : "../uploads/584251f58148e3150fa5c1a7/o-photo_2016-11-09_21-38-55.jpg",
            "_id" : ObjectId("58433bdcf75adf27cb1e8608")
                                        }
                                ]
                        },
    router.get('/obj/:id',  function(req, res) {
    var id = req.params.id;
    
    
    
    Model.findOne({'imgs.other._id': id}, function (err, result) {
            result.imgs.other.id(id).remove();
            result.save();            
        });
    

提交回复
热议问题