繁体   English   中英

如何使用 Mongoose 模式将嵌套数组保存到 Mongodb?

[英]How Save Nested Array into Mongodb Using Mongoose schema?

实际上,只有allFacilities字段在 Schema 中出错,请有人帮助确保allFacilities数组数据的 mongoose 模式allFacilities字段的格式正确

实际上,只有allFacilities字段在 Schema 中出错,请有人帮助确保allFacilities数组数据的 mongoose 模式allFacilities字段的格式正确

我的数据看起来像那样

{
    "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"
            ]
        }
    ]
}

我的猫鼬模式看起来像那样

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)

对房间、照片和所有设施使用“类型:数组”

  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,
  },
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM