0

есть такая схема для колекции ресторанов

const PointSchema = new mongoose.Schema({
  name: { type: String, required: true },
})

const RestaurantSchema = new mongoose.Schema({
  name: {
    type: String,
    require: true
  },
  description: {
    type: String,
    default: ""
  },
  image: {
    type: String,
  },
  rating: {
    type: String
  },
  sale: {
    type: String,
  },
  time: {
    type: String
  },
  minPrice: {
    type: String,
  },
  priceRange: {
    type: String
  },
  categories: [],
  points: [PointSchema]
})

Исходя из этого получаем такую структуру в Mongo введите сюда описание изображения

2 ответа 2

2

А разве не так это должно делаться?

const RestaurantSchema = new mongoose.Schema({
  name: {
    type: String,
    require: true
  },
  description: {
    type: String,
    default: ""
  },
  image: {
    type: String,
  },
  rating: {
    type: String
  },
  sale: {
    type: String,
  },
  time: {
    type: String
  },
  minPrice: {
    type: String,
  },
  priceRange: {
    type: String
  },
  categories: [],
  points: [{type: mongoose.Schema.Types.ObjectId, ref: 'Point'}]
})

Чтоб при запросе find делать вот так:

RestaurantModel.find({}).populate("points")

Тем самым получать не id элемента, а полную информацию

0
const Restaurant = mongoose.model('Restaurant', RestaurantSchema);


Restaurant.find({_id: '1234567890'}).then(console.log);
// или
Restaurant.findById('1234567890').then(console.log);

В mongoose.model передается 2 аргумента: название коллекции (с добавлением s в конце) и раннее созданную schema. Я не знаю название вашей коллекции, так что это привел как пример.

Настоятельно рекомендую вам прочитать документацию по mongoose, дабы в дальнейшем у вас возникало меньше подобных вопросов.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.