103

モデルドキュメントに配列があります。提供したキーに基づいてその配列内の要素を削除してから、MongoDBを更新したいと思います。これは可能ですか?

これが私の試みです:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var favorite = new Schema({
    cn: String,
    favorites: Array
});

module.exports = mongoose.model('Favorite', favorite, 'favorite');

exports.deleteFavorite = function (req, res, next) {
    if (req.params.callback !== null) {
        res.contentType = 'application/javascript';
    }
    Favorite.find({cn: req.params.name}, function (error, docs) {
        var records = {'records': docs};
        if (error) {
            process.stderr.write(error);
        }
        docs[0]._doc.favorites.remove({uid: req.params.deleteUid});

        Favorite.save(function (error, docs) {
            var records = {'records': docs};
            if (error) {
                process.stderr.write(error);
            }
            res.send(records);

            return next();
        });
    });
};

これまでのところ、ドキュメントは検出されますが、削除も保存も機能します。

4

8 に答える 8

158

ドキュメントをロードしてコードを使用して変更しなくても、MongoDBで直接更新を行うこともできます。$pullまたは演算子を使用して$pullAll、配列からアイテムを削除します。

Favorite.updateOne({ cn: req.params.name }, {
    $pullAll: {
        favorites: req.params.deleteUid,
    },
});

配列からオブジェクトを削除するには、

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

(複数のドキュメントにupdateManyを使用することもできます)

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

于 2015-01-13T08:01:32.543 に答える
79

チェックされた回答は機能しますが、MongooseJSの最新版では、pullを使用する必要があります。

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

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

私もそれを見上げていました。

正解については、ダニエルの答えを参照してください。ずっといい。

于 2014-07-24T20:18:43.550 に答える
14

上記の回答は、配列を削除する方法を示しています。ここでは、配列からオブジェクトをプルする方法を示します。

参照: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.
)
于 2018-10-12T06:51:23.557 に答える
6

お気に入りは配列であるため、それをつなぎ合わせてドキュメントを保存するだけです。

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);
    });
};
于 2013-02-08T09:19:44.420 に答える
4

これは私のために働いており、本当にとても役に立ちます。

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
            })
        }
    })
});

名前がであるモデルがありSubCategory、このカテゴリの配列からクーポンを削除したいと思います。カテゴリの配列があるので、を使用しarrOfSubCategoryました。そこで、演算子を使用して、マップ関数を使用してこの配列からオブジェクトの各配列をフェッチします$in

于 2016-09-23T10:45:24.633 に答える
2
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

使用したい場合は、jsファイルの先頭で...呼び出す必要があります。'use strict';:)

于 2016-06-06T04:58:18.730 に答える
2

私は自分のプロジェクトにこのフォーマットを使用しましたが、うまくいきました

router.delete('/dashboard/participant/:id', async (req, res, next) => {
    try {
        const participant = await Participant.findByIdAndDelete({ _id: req.params.id });
        // { $pull: { templates: { _id: templateid } } },
        const event = await Event.findOneAndUpdate({ participants: participant._id }, { $pull: { participants: participant._id } }, { new: true });
        res.status(200).json({ request: 'Deleted', participant, event });
    } catch (error) {
        res.json(error)
    }
});
于 2021-08-18T04:02:04.737 に答える
0
Favorite.update({ cn: req.params.name }, { "$pull": { "favorites": { "_id": favoriteId } }}, { safe: true, multi:true }, function(err, obj) {
    //do something smart
});
于 2022-02-18T12:49:58.100 に答える