簡體   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