2017-07-24 61 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.