最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose - Model.deleteOne() is deleting the entire collection instead of a single document - Stack Overflow

programmeradmin2浏览0评论

I have a User model that contains an array of customers. I want to delete a specific customer based on the customer _id. From what I've read in the Mongoose docs, I should use Model.deleteOne to delete a single document.

Here is my attempt

User Schema (it's been shortened for brevity):

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
    username: {
        type: String,
        default: ''
    },
    password: {
        type: String,
        default: '',
    },
    registerDate: {
        type: Date,
        default: Date.now()
    },
    customer: [{
        name: {
            type: String,
            default: '',
        },
        email: {
            type: String,
            default: 'No email name found'
        },
        fleet: [{
            unitNumber: {
                type: String,
                default: 'N/A',
            }
        }]
    }]
});

module.exports = mongoose.model('User', UserSchema);

Here is a look at the route and controller:

const express = require('express');
const router = express.Router();

const customer_controller = require('../../controllers/customers');

router.delete('/customers/:custid', customer_controller.customer_remove);

  module.exports = router;

And finally the controller:

exports.customer_remove = (req, res) => {
    const { params } = req;
    const { custid } = params;

    User.deleteOne({ 'customer._id': custid }, (err) => {
        if (err)
            throw err;
        else 
            console.log(custid, 'is deleted');
    });
};

From what I thought, User.deleteOne({ 'customer.id': custid }) would find the customer _id matching the custid that is passed in via the req.params. When I test this route in Postman, it deletes the entire User collection that the customer is found in, instead of just deleting the customer. Can I get a nudge in the right direction? I feel like I am close here (or not lol).

I have a User model that contains an array of customers. I want to delete a specific customer based on the customer _id. From what I've read in the Mongoose docs, I should use Model.deleteOne to delete a single document.

Here is my attempt

User Schema (it's been shortened for brevity):

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
    username: {
        type: String,
        default: ''
    },
    password: {
        type: String,
        default: '',
    },
    registerDate: {
        type: Date,
        default: Date.now()
    },
    customer: [{
        name: {
            type: String,
            default: '',
        },
        email: {
            type: String,
            default: 'No email name found'
        },
        fleet: [{
            unitNumber: {
                type: String,
                default: 'N/A',
            }
        }]
    }]
});

module.exports = mongoose.model('User', UserSchema);

Here is a look at the route and controller:

const express = require('express');
const router = express.Router();

const customer_controller = require('../../controllers/customers');

router.delete('/customers/:custid', customer_controller.customer_remove);

  module.exports = router;

And finally the controller:

exports.customer_remove = (req, res) => {
    const { params } = req;
    const { custid } = params;

    User.deleteOne({ 'customer._id': custid }, (err) => {
        if (err)
            throw err;
        else 
            console.log(custid, 'is deleted');
    });
};

From what I thought, User.deleteOne({ 'customer.id': custid }) would find the customer _id matching the custid that is passed in via the req.params. When I test this route in Postman, it deletes the entire User collection that the customer is found in, instead of just deleting the customer. Can I get a nudge in the right direction? I feel like I am close here (or not lol).

Share Improve this question edited Oct 19, 2018 at 2:46 maison.m asked Oct 19, 2018 at 1:04 maison.mmaison.m 8734 gold badges21 silver badges40 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

deleteOne operates at the document level, so your code will delete the first User document that contains a customer element with a matching _id.

Instead, you want update the user document(s) to remove a specific element from the customer array field using $pull. To remove the customer from all users:

User.updateMany({}, { $pull: { customer: { _id: custid } } }, (err) => { ...

Using Mongoose you can do this:

 model.findOneAndUpdate({ 'customer._id': custid }, {$pull: { $pull: { 
 customer: { _id: custid } }}, {new: true}).lean();

Removing subdocs.

Each sub document has an _id by default. Mongoose document arrays have a special id method for searching a document array to find a document with a given _id.

Visit: https://mongoosejs./docs/subdocs.html

parent.children.id(_id).remove();

Use async-await, may be that will work.

exports.customer_remove = async (req, res) => {
    const { params } = req;
    const { custid } = params;
    
    try {
        await User.deleteOne({ 'customer._id': custid });
        console.log(custid, 'is deleted');
    } catch (err) {
        throw err;
    }
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论