Can't rename timestamp fields (createdAt and updatedAt) in MongoDB Compass

This is my schema validator:

{
  $jsonSchema: {
    bsonType: "object",
    required: ["nome", "integrantes"],
    properties: {
      nome: {
        bsonType: "string",
        description: "'nome' deve ser uma string que representa o nome da equipe e precisa ser informado."
      },
      integrantes: {
        bsonType: ["string"],
        description: "'integrantes' deve ser um array de string que representa o(s) nome(s) do(s) integrante(s) da equipe e precisa ser informado.",
        minItems: 1,
        uniqueItems: true
      },
      timestamps: {
        createdAt: "criadaEm",
        updatedAt: "atualizadaEm"
      }
    }
  }
}

And the error is:

Parsing of collection validator failed :: caused by :: Unknown $jsonSchema keyword: createdAt

I am not too familiar with schema-validation but by looking at the first example of the documentation, I think that timestamps, being an object with 2 fields would need to be specified as

timestamps : {
  bsonType: "object" ,
  properties: {
    createdAt: {
      bsonType: "date" ,
      description: "criadaEm"
    }
    updatedAt: {
      bsonType: "date" ,
      description: "atualizadaEm"
    }
  }
}

Unless of course, I misunderstand your intent and what your really want is to have have createdAt and updatedAt as top level attributes like nome and integrantes. If this is the case, then it should be:

    createdAt: {
      bsonType: "date" ,
      description: "criadaEm"
    } ,
    updatedAt: {
      bsonType: "date" ,
      description: "atualizadaEm"
    }

I want to “enable” the timestamps of any document, renaming createdAt to criadaEm and updatedAt to atualizadaEm. I don’t know if it’s possible to do that in the schema validatior.

MongoDB has no default and automatic createdAt and updatedAt fields. Thanks you MongoDB since I do not want to pay performance penalty to a feature that I do not need in some of my use case.

I think mongoose has something in this effect. However I do not know if you can renamed them.

2 Likes

Oh, ok! Thank you for clarifying!

1 Like

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