Node.js 在Mongoose模式中保存数组
假设我想要一个数据模式:每个用户都可以有许多笔记本电脑,有些笔记本电脑可用,有些则不可用。 我希望我能得到一些模式,比如Node.js 在Mongoose模式中保存数组,node.js,mongodb,mongoose-schema,Node.js,Mongodb,Mongoose Schema,假设我想要一个数据模式:每个用户都可以有许多笔记本电脑,有些笔记本电脑可用,有些则不可用。 我希望我能得到一些模式,比如 User | userId: 1 | laptops: | laptopId: 1 available: true //default | laptopId: 2 available: true 如何定义这样的模式? 以下情况不正确: const User = new mongoose.Schema({ userId: { type: St
User
| userId: 1
| laptops:
| laptopId: 1 available: true //default
| laptopId: 2 available: true
如何定义这样的模式?
以下情况不正确:
const User = new mongoose.Schema({
userId: {
type: String,
index: { unique: true }
},
laptops: {
laptopId: String,
available: Boolean
}
});
如何在node.js的Mongoose中定义它?您只需使用方括号,如本例所示:
const User = new mongoose.Schema({
userId: {
type: String,
index: { unique: true }
},
laptops: [{
laptopId:{
type: String
},
available: {
type: Boolean
}
}]
});
检查