Skip to content

MongoDB and Mongoose - Delete Many Documents with model.remove() #43310

@skaparate

Description

@skaparate

Affected page

The lesson at https://www.freecodecamp.org/learn/apis-and-microservices/mongodb-and-mongoose/delete-many-documents-with-model-remove doesn't work with mongoose 6+ because now it just returns the deletedCount, which causes the code from server.js, lines 343 through 351, to fail.

Mongoose 6.0.1
image

Mongoose 5.4.13:
image

This would fix it:

if (data.ok === undefined) {    
    try {
        if ("object" === typeof data && Object.keys(data).length === 1) {
            // for mongoose v6
            data.ok = true;
            data.n = data.deletedCount;
        } else {
            // for mongoose v4
            data = JSON.parse(data);
        }
    } catch (e) {
        console.log(e);
        return next(e);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    first timers onlyOpen for anyone doing contributions for first time. You do not need permission to work on these.scope: curriculumLessons, Challenges, Projects and other Curricular Content in curriculum directory.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions