Mongoose - `strict` option not working in findOneAndReplace

I am trying to implement PUT and PATCH request using Mongoose,the code for handing both requests are as follows:

function colCon(){
    /* ... retrieve mongoose model ... */
}

function putData(_title = '', newData = {}){
    console.log('> put data');

    const condition = {title : _title};
    return colCon().then(async model => {
        return model.findOneAndReplace(
            condition, 
            newData,
            {
                returnDocument : 'after',
                strict : false
            }
        ).then(doc => {
            return doc;
        }).catch(err => {
            throw err;
        })
    }).catch(err => {
        throw err;
    })
}

function patchData(_title = '', newData = {}){
    console.log('> patch data');

    const condition = {title : _title};
    return colCon().then(async model => {
        return model.findOneAndUpdate(
            condition, 
            newData,
            {
                returnDocument : 'after',
                strict: false
            }
        ).then(doc => {
            return doc;
        }).catch(err => {
            throw err;
        })
    }).catch(err => {
        throw err;
    })
}

While both have similar approaches (retrieve model → find and update / replace → show after-modified result), the results are quite different. For example this is my intial schema and document

//schema
{
    title: {
        type: String,
        unique: true,
        required: true,
    },
    
    content: { 
        type: String,  
    }
}

//document sample 
{
    "content": "Something here",
    "title": "Some title"
}

And this is the result after I update / replace with the same new data

//new data
{
    "content": "Something new here",
    "title": "Some title"
    "car": "bugati"
}

//PATCH result
{
    "content": "Something new here",
    "title": "Some title",
    "car": "bugati"
}

//PUT result
{
    "content": "Something new here",
    "title": "Some title"
}

Although I have turn off strict mode in both functions, extra fields is still prevented in PUT request.

How can I disable it in PUT function the same as PATCH?

Thank you in advance!

Hey @D_n_Du,

Thank you for reaching out to the MongoDB Community forums :sparkles:

Based on my understanding, I believe Mongoose does not handle the PUT and PATCH methods. Typically, these methods are handled by web frameworks and server-side technologies.

Regarding the difference in output between the findOneAndUpdate and findOneAndReplace methods, it appears that the issue might be related to the strict option not being properly getting patched in for findOneAndReplace() method due to some reason the mongoose is implemented. I can confirm that I’ve encountered the same issue when testing it in my own environment. To address this issue, I have created a GitHub issue - 13507 on the Mongoose repository.

In the meantime, you can achieve your goal by using the {strict: false} option in your schema. This will ensure that both functions work as you expect them to be:

// Define the schema for your data
const YourSchema = new mongoose.Schema({
    title: String,
    // ... add other properties as needed
}, { strict: false });

Note: Remove the strict option from both functions within your code.

I hope this helps! If you have any further questions, feel free to ask.

Best regards,
Kushagra