Home > Mobile >  How Save Nested Array into Mongodb Using Mongoose schema?
How Save Nested Array into Mongodb Using Mongoose schema?

Time:06-15

Actually, Only allFacilities field makes an error in Schema, Please Someone help by making sure the correct format of the mongoose schema allFacilities field for allFacilities array data

Actually, Only allFacilities field makes an error in Schema, Please Someone help by making sure the correct format of the mongoose schema allFacilities field for allFacilities array data

My data Look Like That

{
    "name": "This is Name countru",
    "type": "Hotel",
    "country": "Bangladesh",
    "city": "Chittagong",
    "address": "22 D Block, Kolpolok Residense, Bakulia,",
    "distance": "Chittagong",
    "title": "tis is best hotel",
    "desc": "Pamper yourself with a visit to the spa, which offers massages, body treatments, and facials. You can take advantage of recreational amenities such as an outdoor pool, a spa tub, and a sauna. Additional features at this hotel include complimentary wireless Internet access, concierge services, and a banquet hall. Grab a bite at The Exchange, one of the hotel's 3 restaurants, or stay in and take advantage of the 24-hour room service. Relax with your favorite drink at the bar/lounge or the poolside bar. Featured amenities include a business center, dry cleaning/laundry services, and a 24-hour front desk. Free valet parking is available onsite. Make yourself at home in one of the 241 air-conditioned rooms featuring LED televisions. Complimentary wireless Internet access keeps you connected, and satellite programming is available for your entertainment. Private bathrooms with separate bathtubs and showers feature hair dryers and slippers. Conveniences include phones, as well as safes and coffee/tea makers. Distances a",
    "cheapestPrice": "105",
    "rooms": [
        "629dd3bae549560b971612da",
        "629dd418e549560b971612df",
        "629dd481e549560b971612ea",
        "629dd4b3e549560b971612f0",
        "629dd4b4e549560b971612f6",
        "629dec61e549560b9716139f"
    ],
    "photos": [
        "http://res.cloudinary.com/cloudmonzu/image/upload/v1655222493/upload/jxsbi7r26cyqgnwg65ni.jpg",
        "http://res.cloudinary.com/cloudmonzu/image/upload/v1655222494/upload/bybbcdb8iluy0t7smf1u.jpg",
        "http://res.cloudinary.com/cloudmonzu/image/upload/v1655222493/upload/pzfzk3umv5agumt30ygo.jpg"
    ],
    "allFacilities": [
        {
            "hotelfacilities": [
                "dsds",
                " sdsd",
                " sdd"
            ]
        },
        {
            "roomfacilities": [
                "sdsd",
                " dss",
                " dss"
            ]
        },
        {
            "wellnessSpa": [
                "sds",
                " dsd",
                " sdsd",
                " sds"
            ]
        }
    ]
}

My Mongoose Schema Look Like That

import mongoose from "mongoose";
const HotelSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  country: {
    type: String,
    required: true,
  },
  city: {
    type: String,
    required: true,
  },

  address: {
    type: String,
    required: true,
  },
  distance: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  desc: {
    type: String,
    required: true,
  },
  cheapestPrice: {
    type: Number,
    required: true,
  },
  rooms: {
    type: [String],
  },
  photos: {
    type: [String],
  },
  allFacilities: {
    type: [String],
    required: true,
  },

  rating: {
    type: Number,
    min: 0,
    max: 5,
  },
  featured: {
    type: Boolean,
    default: false,
  },
});

export default mongoose.model("Hotel", HotelSchema)

CodePudding user response:

use "type: Array" for rooms, photos and allFacilities

  name: {
    type: String,
    required: true,
  },
  type: {
    type: String,
    required: true,
  },
  country: {
    type: String,
    required: true,
  },
  city: {
    type: String,
    required: true,
  },

  address: {
    type: String,
    required: true,
  },
  distance: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  desc: {
    type: String,
    required: true,
  },
  cheapestPrice: {
    type: Number,
    required: true,
  },
  rooms: {
    type: Array,
  },
  photos: {
    type: Array,
  },
  allFacilities: {
    type: Array,
    required: true,
  },

  rating: {
    type: Number,
    min: 0,
    max: 5,
  },
  featured: {
    type: Boolean,
    default: false,
  },
});

  • Related