2015-05-07 68 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