gpt4 book ai didi

mongodb - Mongoose 模式,如何在一个模式中嵌套对象?

转载 作者:可可西里 更新时间:2023-11-01 09:11:16 25 4
gpt4 key购买 nike

在我的 Mongoose 模式中,我定义了一些数据类型和两个对象数组。第一个对象菜应该没问题。

但在第二个嵌套对象顺序中,我想将第一个对象 dish 包含进去,但我不知道如何正确执行此操作。

module.exports = function( mongoose) {
var ShopSchema = new mongoose.Schema({
shopName: { type: String, unique: true },
address: { type: String},
location:{type:[Number],index: '2d'},
shopPicUrl: {type: String},
shopPicTrueUrl:{type: String},
mark: { type: String},
open:{type:Boolean},
shopType:{type:String},

dish: {type: [{
dishName: { type: String},
tags: { type: Array},
price: { type: Number},
intro: { type: String},
dishPic:{ type: String},
index:{type:Number},
comment:{type:[{
date:{type: Date,default: Date.now},
userId:{type: String},
content:{type: String}
}]}
}]},

order:{type:[{
orderId:{type: String},
date:{type: Date,default: Date.now},
dish:{type: [dish]},//!!!!!!!!! could I do this?
userId:{type: String}
}]}

});

最佳答案

这是设计模型的正确方法

var mongoose = require('mongoose');
Schema = mongoose.Schema;

var DishSchema = new mongoose.Schema({
dishName: { type: String },
tags: { type: Array },
price: { type: Number },
intro: { type: String },
dishPic: { type: String },
index: { type: Number },
comment: { type: [{
date: {type: Date, default: Date.now },
userId: {type: String },
content: {type: String }
}]}
});

var ShopSchema = new mongoose.Schema({
shopName: { type: String, unique: true },
address: { type: String },
location: { type: [Number], index: '2d' },
shopPicUrl: { type: String },
shopPicTrueUrl: { type: String },
mark: { type: String },
open: { type: Boolean },
shopType: { type: String },
dish: { type: [DishSchema] },
order: { type: [{
orderId: { type: String },
date: { type: Date, default: Date.now },
dish: { type: [DishSchema] },
userId: { type: String }
}]}
});

var Shop = mongoose.model('Shop', ShopSchema);
module.exports = Shop;

关于mongodb - Mongoose 模式,如何在一个模式中嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com