Mongoose delete array element in document and save

后端 未结 6 410
盖世英雄少女心
盖世英雄少女心 2020-11-28 23:53

I have an array in my model document. I would like to delete elements in that array based on a key I provide and then update MongoDB. Is this possible?

Here\'s my

相关标签:
6条回答
  • 2020-11-29 00:35

    Answers above are shown how to remove an array and here is how to pull an object from an array.

    Reference: https://docs.mongodb.com/manual/reference/operator/update/pull/

    db.survey.update( // select your doc in moongo
        { }, // your query, usually match by _id
        { $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } }, // item(s) to match from array you want to pull/remove
        { multi: true } // set this to true if you want to remove multiple elements.
    )
    
    0 讨论(0)
  • You can also do the update directly in MongoDB without having to load the document and modify it using code. Use the $pull or $pullAll operators to remove the item from the array :

    Favorite.updateOne( {cn: req.params.name}, { $pullAll: {uid: [req.params.deleteUid] } } )
    

    (you can also use updateMany for multiple documents)

    http://docs.mongodb.org/manual/reference/operator/update/pullAll/

    0 讨论(0)
  • 2020-11-29 00:43
    keywords = [1,2,3,4];
    doc.array.pull(1) //this remove one item from a array
    doc.array.pull(...keywords) // this remove multiple items in a array
    

    if you want to use ... you should call 'use strict'; at the top of your js file; :)

    0 讨论(0)
  • 2020-11-29 00:48

    Since favorites is an array, you just need to splice it off and save the document.

    var mongoose = require('mongoose'),
        Schema = mongoose.Schema;
    
    var favorite = new Schema({
        cn: String,
        favorites: Array
    });
    
    module.exports = mongoose.model('Favorite', favorite);
    
    exports.deleteFavorite = function (req, res, next) {
        if (req.params.callback !== null) {
            res.contentType = 'application/javascript';
        }
        // Changed to findOne instead of find to get a single document with the favorites.
        Favorite.findOne({cn: req.params.name}, function (error, doc) {
            if (error) {
                res.send(null, 500);
            } else if (doc) {
                var records = {'records': doc};
                // find the delete uid in the favorites array
                var idx = doc.favorites ? doc.favorites.indexOf(req.params.deleteUid) : -1;
                // is it valid?
                if (idx !== -1) {
                    // remove it from the array.
                    doc.favorites.splice(idx, 1);
                    // save the doc
                    doc.save(function(error) {
                        if (error) {
                            console.log(error);
                            res.send(null, 500);
                        } else {
                            // send the records
                            res.send(records);
                        }
                    });
                    // stop here, otherwise 404
                    return;
                }
            }
            // send 404 not found
            res.send(null, 404);
        });
    };
    
    0 讨论(0)
  • 2020-11-29 00:50

    The checked answer does work but officially in MongooseJS latest, you should use pull.

    doc.subdocs.push({ _id: 4815162342 }) // added
    doc.subdocs.pull({ _id: 4815162342 }) // removed
    

    https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-pull

    I was just looking that up too.

    See Daniel's answer for the correct answer. Much better.

    0 讨论(0)
  • 2020-11-29 00:53

    This is working for me and really very helpful.

    SubCategory.update({ _id: { $in:
            arrOfSubCategory.map(function (obj) {
                return mongoose.Types.ObjectId(obj);
            })
        } },
        {
            $pull: {
                coupon: couponId,
            }
        }, { multi: true }, function (err, numberAffected) {
            if(err) {
                return callback({
                    error:err
                })
            }
        })
    });
    

    I have a model which name is SubCategory and I want to remove Coupon from this category Array. I have an array of categories so I have used arrOfSubCategory. So I fetch each array of object from this array with map function with the help of $in operator.

    0 讨论(0)
提交回复
热议问题