首頁  >  問答  >  主體

mongoose - mongodb, 商家資料庫schema如何建立?

簡化來說,一個shop一般有一些屬性類似:
那麼在對這個schema進行操作的時候,通常是有一個帳戶來存取node.js裡面的路由。

例如帳號名稱是shopName,密碼是password

但是現實情況是,一個店舖可以有好幾個帳號,帳號之間的權限會不一樣。比如,店小二的帳號權限只能新增一個商品。而老闆的帳號權限可以刪除一個訂單。

這個在mongodb裡面實現的大致思路是什麼呢?

module.exports = function( mongoose) {
  var ShopSchema = new mongoose.Schema({
    shopName:     { type: String, unique: true },
    password:{type:String},
    address:     { type: String},
    location:{type:[Number],index: '2d'},
    shopPicUrl:      {type: String},
    shopPicTrueUrl:{type: String},
    mark:  { type: String},
    open:{type:Boolean},
    shopType:{type:String},
    
    dish:{type:[DishSchema]},

    order:{type:[{
      orderId:{type: String},
      date:{type: Date,default: Date.now},
      dish:{type: [DishSchema]},
      userId:{type: String}
    }]}
    
  });

var DishSchema = new mongoose.Schema({
  dishName: { type: String},
      tags: { type: Array},
      price: { type: Number},
      intro: { type: String},
      dishPic:{ type: String},
      index:{type:Number},
      comment:{type:[{
        date:{type: Date,default: Date.now},
        userId:{type: String},
        content:{type: String}
      }]}
    });
習慣沉默習慣沉默2715 天前769

全部回覆(1)我來回復

  • ringa_lee

    ringa_lee2017-05-02 09:21:30

    店鋪是一個單獨的帳號,下面搞一個數組,紀錄用戶名密碼和登陸權限。
    {
    shopname:XXXXX,
    accouts:[
    {

    loginname:xxxx,
    password:xxxx,
    authority:[],

    }]
    }
    然後建立一個索引 accouts.loginname unique 的
    用mongo了就用mongo的方式思考,現在的ODM很多還是ORM的思路設計的,我傾向不用。直接寫json
    當然,你可以把使用者的詳細資料塞到loginname那一層例如頭像介紹什麼的,另外建表也行

    回覆
    0
  • 取消回覆