0

I am new to Mongo, so excuse me if this a dumb question.

I know that Mongo inserts an _id on the parent level object as a primary key, but is it normal for it to insert an _id for every field, or have I made some sort of mistake.

Also what is the __v field?

enter image description here

Here is how I'm forming this object:

Schema /models/Restaurant.js

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

const Restaurants = new Schema({
    name: String,
    categories: [{
        sandwiches: [{
            name: String,
            description: String,
            img: String,
            price: String
        }]
    }]
})
module.exports = mongoose.model('Restaurants', Restaurants);

And where I am seeding it:

seed.js

const rest = new Restaurants({
    name: "taco bell",
    categories: [{
        sandwiches: [{
            name: "breadedChickenFlatbread",
            description: "A good sandwich",
            img: "./sandwich.jpg",
            price: "$8.99"
        }]
    }]
});

rest.save(() => {})

Are all the _id fields I am seeing, normal? Also, where is the __v field coming from?

Edon
  • 1,116
  • 2
  • 22
  • 47

1 Answers1

0

Yeah, it looks normal.

The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero (__v:0).

If you don't want to use this version key you can use the versionKey: false as mongoose.Schema parameter.

You can follow this example...

const Schema = mongoose.Schema;

const Restaurants = new Schema({
    name: String,
    categories: [{
        sandwiches: [{
            name: String,
            description: String,
            img: String,
            price: String
        }]
    },{
        versionKey: false, // Here You have to add, you should be aware of the outcome after set to false
    }]
})
module.exports = mongoose.model('Restaurants', Restaurants);
Rizwan
  • 363
  • 3
  • 10