2017-05-20 30 views
0

只是爲了方便,我正在爲UberEAT創建一個類似的應用程序。如何根據用戶的輸入創建可以改變的模式?

數據庫可能會達到收集限制,所以我創建了兩個單獨的集合。

首先,我需要的餐廳信息,所以我的結構是這樣的:

var PlaceSchema = new Schema({ 
    id: Number, 
    menuID: Number, 
    name: String, 
    cuisine_type: String, (eg; thai, korean, sushi ,etc) 
    address: String, 
    opening_time:Date, 
    closing_time:Date 
}); 

然後我需要顯示菜單的餐廳都有,問題是每家餐廳都有不同數量的菜餚和價格,所以數量不是恆定的。

這是我如何組織我的菜單數據,但我知道這是完全錯誤的

var MenuSchema = new Schema({ 
    id: Number, 
    parentID: Number, 
    name: String, 
    price: Number 

}); 

任何指導,將是巨大的!

回答

1

這個怎麼樣來獲取酒樓

var PlaceSchema = new Schema({ 
    id: Number, 
    menusId: [Number], 
    name: String, 
    cuisine_type: String, (eg; thai, korean, sushi ,etc) 
    address: String, 
    opening_time:Date, 
    closing_time:Date 
}); 

和菜單

var MenuSchema = new Schema({ 
    id: Number, 
    placeId: Number, 
    dishes: [{name: String, price: Number}] //add any other dish related stuff here, like spiceness, app, entree, desert chef special etc etc 
}); 

現在你可以有一個地方多個菜單,並在菜單多菜開始

。 你甚至可以在菜單中做一些地方的Id,這可能是一堆餐館相同的菜單的情況。

var MenuSchema = new Schema({ 
    id: Number, 
    placesId: [Number], 
    dishes: [{name: String, price: Number}] //add any other dish related stuff here, like spiceness, chef special etc etc 
}); 

更新了飲料默認 這裏是一個更新的菜單模式與飲料 VAR MenuSchema =新mongoose.Schema({ ID:數字, placeId:數字, 菜:[{名稱:串,價格:數}],//在這裏添加任何其他的菜相關的東西,像spiceness,應用程序,主菜,沙漠廚師特等等等等

drinks:[{ 
    name: String, 
    price: Number 
    }] 
}); 

就像我在評論說,貓鼬並沒有真正有任何def陣列。 但是我們可以寫一個預存儲鉤來處理這個問題。

MenuSchema.pre("save", function (next) { 
    if (!this.drinks.length) { 
     this.drinks.push({name: "No Drinks", price: 0}); //null or 0 whichever you pefer 
    } 
    next(); 
}); 

這將默認在默認對象中,如上所述。

P.S.這種違約飲品「不喝酒」並不覺得正確。也許把它保留爲一個空數組,當我們需要在代碼中使用它,顯示或任何只是檢查長度。

if (!menu.drinks.length) { 
    console.log("No drinks"); 
    } 
+0

謝謝!但是,如何將菜餚插入菜單模式?我試過 '盤子:[ 「Panini」,3.50, 「華夫餅」,2.50]'但它沒有工作。 –

+0

你會菜[{名稱:帕尼尼,成本:3.5},{名稱:華夫餅乾,成本:2.5}] –

+0

它的作品:)!最後一件事,如果你要在模式中添加一個默認值,你會怎麼做?我嘗試了'drinks:{type:[{name:String,price:Number}],默認值:「No drinks」},並且它不起作用。 –

相關問題