Search code examples
javascriptmongodbmongoosemern

Mongoose creating __v field instead of date field


I have this model:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ModelNameSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "users",
  },
  date: {
    type: Date,
    dafault: Date.now,
  },
});

module.exports = ModelName = mongoose.model(
  "model_name",
  ModelNameSchema
);

I tried to create documents using this:

const saveDocument = function saveDocument(user_id) {
  const document_data = {
    user: user_id,
  };
  const new_document = new ModelName(document_data);
  const document = new_document.save();
  return document;
};

For some strange reason, instead of creating the date field, it creates the __v field.
Here is an example of two documents I have created:

[
    {
        "_id": "60eb01a29e84151343183f4d",
        "__v": 0
    },
    {
        "_id": "60eb03ccc465491984b3bf99",
        "__v": 0
    }
]

Any idea what's going on here?


Solution

  • In your schema, you have a typo of d**a**fault instead of default

    The __v is simply a version number for the document, so you can negate it from your search queries using .select(-__v)