Why does Mongoose remove the key when specified in the schema?

I have spent the past hour or two puzzled over why my data doesn’t seem to be coming from the database. I’ve narrowed it down to being the result of the schema, but I’m stumped as to how I get around that.

Here’s what happens:
When I specify the schema for an array of objects (may be doing this wrong), that particular key and all the data with it get stripped from any query result. When I remove this from the schema, it suddenly works again.

Here’s why I can’t just keep it out: I need an ObjectId generated for all objects in the array, and only way I can do that without manually supplying one is by using a subdocument schema. I’ve tried reading into documentation, looking this up various ways, looking at examples, and none of it seems to be giving me the solution I’m looking for. I can insert data just fine and validation passes, but it just gets removed when trying to read it.


const Form = new Schema({
    schema: { type: Object, required: true }, // This is a form schema unrelated to mongo
    title: { type: String, required: false },
    subtitle: { type: String, required: false },
    acceptingResponses: { type: Boolean, required: false },
    userSubmissions:  [ // Ends up being an array with objects each containing a key and following this schema
        new Schema({
            status: { type: String, required: true },
            response: { type: Object, required: true },
            respondant: { type: Object, required: true },
            _id: true,
            required: true
    strict: false

I’ve also tried setting userSubmissions to { type: Array, required: true } with the same issue.

1 Like

Hi @Randall_Barker,
I tried to reproduce your issue, but the validation got failed while inserting can you help me by sharing the following details in order to better diagnose the issue?

  1. The version of mongoose you are using in your application server
  2. The version of MongoDB instance that you are connecting your app with
  3. The version of Node.js running your application server

I am assuming you mean the array of userSubmissions by "it" here, is that correct?
Can you also share the commands you use to create and query the Form documents or a self-contained code example that reproduces this issue?

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,

1 Like

I may have left this detail out that may have been very important. Insertion is done on a push of data that was manually entered, not entered through mongoose. Data is pushed into the userSubmissions array which may be skipped on validation, unsure of this behavior.

models.Forms.updateOne({_id: formId},{$push: {userSubmissions: responseData}}).then(resolve).catch(reject);

After one submission, the array looks as follows:

"userSubmissions": [{
        "respondant": {
            "id": "62436ac8a19e9b08e18dab88",
            "userId": 12345,
            "username": "######"
        "status": "pending",
        "response": {
            "question1": ["answer3","answer1","answer7"],
            "question2": "answer4",
            "question3": "being translucent"

For whatever reason, I cannot edit my reply.
My wording may have been slightly confusing. The document was created through manual entry into MongoDB Compass. Responses are pushed into the array. If this isn’t validated when being pushed in, that may very well explain why I’m not receiving any error.

Hi @Randall_Barker,
I have reproduced the issue and it seems like mongoose indeed doesn’t return the array when a key named "schema" exists in the document.
I have raised a similar issue a few days back about the "schema" key causing failed validations even with valid documents. And this issue has already been added to v6.6.6 milestones, check out the following to learn more:

However, when I change the key name: "schema" to something else, it starts working perfectly fine.
Therefore, as a quick fix if you could change the key name "schema" to "formSchema" or uppercased "Schema", it will work as expected.

Also, please avoid skipping mongoose’s schema validation process, and don’t insert documents manually unless that’s really needed.

If you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.