2016-04-26 26 views

回答

0

首先,您需要了解mongodb中架構的基本結構及其定義,以及它如何從mysql轉換。

讓我們來看看幾個defintions enter image description here如果你有兩個表,因爲你給了,用戶角色和用戶的詳細信息

,你的潛在MongoDB的架構應如下

{ 
    _id: objectid ("random alphanumeric"), 
    username: "John Doe", 
    firstname: "John", 
    lastname: "Doe", 
    title: "Manager", 
     Role: [ 
       { 
        id: "Random alphanumeric", 
        type: "Admin" 
       } 
      ] 
} 

請也經歷sqldb映射的Mongodb文檔 https://docs.mongodb.org/manual/reference/sql-comparison/

注意:這裏_id指的是您的用戶標識。

0

您可以在同一way.I用於遵循同樣的模式

角色schema-

var rolesSchema = new mongoose.Schema({ 
 
    'role_name':{type:String}, 
 
    'actions':[{type:String}] 
 
})

用戶模式 ​​-

var userSchema = new mongoose.Schema({ 
 
    first_name : String, 
 
    last_name : String, 
 
    role: String, // OR { type: mongoose.Schema.Types.ObjectId, ref: 'role'} 
 
})

並且您可以直接使用填充貓鼬爲該用戶獲取角色數據或手動使用角色將其作爲字符串進行檢查。

相關問題