Storing an Array in a Mongoose Field: A Guide

Mon Jan 3, 2022

In Mongoose, you can store an array in a field by defining the field as an array type in your schema. For example:

const mongoose = require('mongoose'); const schema = new mongoose.Schema({ myArray: [Number] }); const MyModel = mongoose.model('MyModel', schema);

This will create a new Mongoose model called "MyModel" with a field called "myArray" that can store an array of numbers.

You can also use the mongoose.Schema.Types.Array to define the array field

const mongoose = require('mongoose'); const schema = new mongoose.Schema({ myArray: {type: mongoose.Schema.Types.Array} }); const MyModel = mongoose.model('MyModel', schema);

This will create a new Mongoose model called "MyModel" with a field called "myArray" that can store any array.

DCT Academy
Full Stack web development training institute in Bangalore

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
Best MERN Full Stack Development Course | Bangalore | Offline | Online | 100% Placements | Training Institute | Front End Course | React JS Training | Online Course India 2024 Privacy policy Terms of use Contact us Refund policy