How to create nested fields in a schema?

Lets say that I have this user schema that has a inventory with multiple slots. How could I nest those slots in to the inventory?

For example like this:

const UserSchema = new mongoose.Schema({
Inventory: {
slot1img: {
type: String
},
slot1text: {
type: String
},
slot2img: {
type: String
},
slot2text: {
type: String
},
slot3img: {
type: String
},
}
})

However if I try it like this it wont show up at all in the database, so how do i do this?

I think that you would want to use the embedded document pattern to create a subset inside of Inventory.

Below is the link to the Mongoose documenation on sub documents.
Mongogoose docs on sub documents

1 Like