There are no items in your cart
Add More
Add More
| Item Details | Price | ||
|---|---|---|---|
{{DATE}}
To store an array of objects in a Mongoose field, you can define the field as an array of a specific sub-schema.
const mongoose = require('mongoose'); const subSchema = new mongoose.Schema({ name: String, age: Number });const schema = new mongoose.Schema({ myArray: [subSchema] }); 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 objects, where each object must have a "name" field of type String and an "age" field of type Number.
You can also use mongoose.Schema.Types.ObjectId to define an array of objects with ids
const mongoose = require('mongoose'); const subSchema = new mongoose.Schema({ _id: mongoose.Schema.Types.ObjectId, name: String, age: Number });const schema = new mongoose.Schema({ myArray: [subSchema] }); 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 objects, where each object must have a "name" field of type String, an "age" field of type Number and an _id field of type ObjectId
You can then use the Mongoose model to interact with the database and perform CRUD operations on the array of objects.

{{AUTHOR}}
Full Stack web development training institute in Bangalore