在 Mongoose 模式中保存数组 [英] Save arrays in Mongoose schema
本文介绍了在 Mongoose 模式中保存数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想要一个数据模式:每个用户可以拥有多台笔记本电脑,有些笔记本电脑可用,有些则不可用.我希望我能得到一些像,
Suppose I want to have a data schema: every user can have many laptops, and some laptops are available some are not. I hope that I can get some schema like,
User
| userId: 1
| laptops:
| laptopId: 1 available: true //default
| laptopId: 2 available: true
我如何定义这样的架构?以下不正确:
How can I define such schema? The following is not correct:
const User = new mongoose.Schema({
userId: {
type: String,
index: { unique: true }
},
laptops: {
laptopId: String,
available: Boolean
}
});
如何在 node.js 的 Mongoose 中定义这个?
How to define this in the Mongoose in node.js?
推荐答案
你只需使用方括号,就像这个例子:
You simply use square brackets, like in this example:
const User = new mongoose.Schema({
userId: {
type: String,
index: { unique: true }
},
laptops: [{
laptopId:{
type: String
},
available: {
type: Boolean
}
}]
});
这篇关于在 Mongoose 模式中保存数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文