skip to Main Content
[
const arrayOfObjectsSchema = new Schema({
  array: [
    {
      title: {
        type: String,
        required: true,
      },
      desc: {
        type: String,
        required: true,
      },
      img: {
        type: String,
        required: true,
      },
      content: {
        type: String,
        required: true,
      },
      username: {
        type: String,
        required: true,
      },
    },
  ],
});

const arrayOfObjectsSchema = new Schema([
    {
      title: {
        type: String,
        required: true,
      },
      desc: {
        type: String,
        required: true,
      },
      img: {
        type: String,
        required: true,
      },
      content: {
        type: String,
        required: true,
      },
      username: {
        type: String,
        required: true,
      },
    },
  ],
);


Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

ignore this,… stackoverflow wont let me post

these are the schemas I used.. they do not work and I dont know why I just need to upload array of objects but nothing works help…

2

Answers


  1. You can use this format to create an array of object with mongoose model

    const arrayOfObjectsSchema = new mongoose.Schema({
        title: {
            type: String,
            required: true,
        },
        datas: [{
            id: {
                type: String,
                required: true,
            },
            info: {
                type: String,
                required: true,
            }
        }]
    });
    
    Login or Signup to reply.
  2. Hope it help!

    const mongoose = require('mongoose');
    
    // Define the book schema
    const bookSchema = new mongoose.Schema({
      title: {
        type: String,
        required: true,
      },
      author: {
        type: String,
        required: true,
      },
      genre: {
        type: String,
        required: true,
      },
    });
    
    // Define the main schema containing the array of books
    const librarySchema = new mongoose.Schema({
      name: {
        type: String,
        required: true,
      },
      books: [bookSchema], // The 'books' field is an array of objects based on the 'bookSchema'
    });
    
    // Create the 'Library' model using the 'librarySchema'
    const Library = mongoose.model('Library', librarySchema);
    
    // Example usage:
    // Create a new library with books
    const newLibrary = new Library({
      name: 'Awesome Library',
      books: [
        { title: 'Book 1', author: 'Author 1', genre: 'Fiction' },
        { title: 'Book 2', author: 'Author 2', genre: 'Non-Fiction' },
        { title: 'Book 3', author: 'Author 3', genre: 'Mystery' },
      ],
    });
    
    // Save the new library to the database
    newLibrary.save()
      .then((savedLibrary) => {
        console.log('Library with books saved:', savedLibrary);
      })
      .catch((error) => {
        console.error('Error saving library:', error);
      });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search