2017-07-24 58 views
0

假设我想要一个数据模式:每个用户可以有多个笔记本电脑,并且有些笔记本电脑可用。 我希望我可以得到一些架构一样,将数组保存在Mongoose模式中

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中的猫鼬定义呢?

回答

1

您只需在这个例子中使用方括号,如:

const User = new mongoose.Schema({ 
    userId: { 
    type: String, 
    index: { unique: true } 
    }, 
    laptops: [{ 
    laptopId:{ 
     type: String 
    }, 
    available: { 
     type: Boolean 
    } 
    }] 
}); 

检查official docs for allowed SchemaTypes.