2015-05-07 108 views
0

我使用meanjs,我想存儲一對多關係的用戶數據。我的情況與文章示例類似,但文章只能通過用戶訪問。我想要的路線是這樣的mongodb用戶與子文檔與用戶Id的文檔

Users/:userId/articles 

Users/me/articles 

問題1 如果我只是用物品模型堅持,因爲它是或者我應該做的文章的用戶子文檔。例如

var UserSchema = new Schema({ 
    firstName: { 
     type: String, 
     trim: true, 
     default: '', 
     validate: [validateLocalStrategyProperty, 'Please fill in your first name'] 
    }, 
    lastName: { 
     type: String, 
     trim: true, 
     default: '', 
     validate: [validateLocalStrategyProperty, 'Please fill in your last name'] 
    }, 
    displayName: { 
     type: String, 
     trim: true 
    }, 
    email: { 
     type: String, 
     trim: true, 
     default: '', 
     validate: [validateLocalStrategyProperty, 'Please fill in your email'], 
     match: [/.+\@.+\..+/, 'Please fill a valid email address'] 
    }, 
    username: { 
     type: String, 
     unique: 'testing error message', 
     required: 'Please fill in a username', 
     trim: true 
    }, 
    articles: [articleModel.schema], 
    password: { 
     type: String, 
     default: '', 
     validate: [validateLocalStrategyPassword, 'Password should be longer'] 
    }, 
    salt: { 
     type: String 
    }, 
    provider: { 
     type: String, 
     required: 'Provider is required' 
    }, 
    providerData: {}, 
    additionalProvidersData: {}, 
    roles: { 
     type: [{ 
      type: String, 
      enum: ['user', 'store', 'admin'] 
     }], 
     default: ['user'] 
    }, 
    updated: { 
     type: Date 
    }, 
    created: { 
     type: Date, 
     default: Date.now 
    }, 
    /* For reset password */ 
    resetPasswordToken: { 
     type: String 
    }, 
    resetPasswordExpires: { 
     type: Date 
    } 
}); 

問題2如果我使它成爲一個子文檔,我仍然可以使用$資源功能,或者我必須製作自定義函數嗎?

+3

專家建議不要嵌入將無限增長的數據,就像用戶的文章一樣。 – MFB

+0

謝謝,這使我更容易編碼。 – Leo

+0

最大BSON文檔大小是16兆字節。最大文檔大小有助於確保單個文檔不能使用過多的RAM,或者在傳輸過程中使用過多的帶寬。爲了存儲大於最大大小的文檔,MongoDB提供了GridFS API。 – HDK

回答

1

最大BSON文檔大小是16兆字節。最大文檔大小有助於確保單個文檔不能使用過多的RAM,或者在傳輸過程中使用過多的帶寬。爲了存儲大於最大大小的文檔,MongoDB提供了GridFS API。

+0

那麼這是否意味着如果文章大小可能超過16MB,我應該將其作爲單獨的文檔而不是子文檔? – Leo