如何使猫鼬和mongodb正常工作?

时间:2018-11-13 05:48:24

标签: javascript mongodb express mongoose

我试图从模型内部的数组中拉出对象。但是我无法使其正常工作。我已经检查了我的查询参数,所以我知道它们正在输出正确的值。任何帮助,将不胜感激!

模式:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const collectionSchema = new Schema({
    type: String,
    name: String,
    id: String,
    gamesCollected: [
        {
            id: Number,
            name: String,
            summary: String,
            first_release_date: Number,
            screenshots: [
                {
                    url: String,
                    couldinary_id: String,
                    width: Number,
                    height: Number
                }
            ],
            cover: {
                url: String,
                couldinary_id: String,
                width: Number,
                height: Number
            },
            platfroms: [
                Number
            ]

        }
    ]
});

mongoose.model('collection', collectionSchema);

路线:

router.delete('/delete_game', (req, res) => {
    Collection.findOneAndUpdate({_id: req.query.collectionID}, {$pull: 
    {gamesCollected: {_id: req.query.id}}});
    res.end();
});

1 个答案:

答案 0 :(得分:1)

请用req.body替换req.query

router.delete('/delete_game', (req, res) => {
    Collection.findOneAndUpdate({_id: req.body.collectionID}, {$pull: 
    {gamesCollected: {_id: req.body.id}}});
    res.end();
});

再次提及类似的问题How to get parameter for delete request in express node js