mongodb用户使用子文档vs使用UserId的文档

时间:2015-05-07 22:08:19

标签: angularjs mongodb express mongoose meanjs

我正在使用meanjs,我想将一些用户数据存储在一对多的关系中。我的案例类似于文章示例,但文章只能通过用户访问。我希望路线像

Users/:userId/articles

Users/me/articles

问题1 我应该坚持使用文章模型,还是应该将文章作为用户的子文档。 e.g。

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如果我将其作为子文档,我仍然可以使用$ resource函数还是我必须自定义函数?

1 个答案:

答案 0 :(得分:1)

最大BSON文档大小为16兆字节。最大文档大小有助于确保单个文档不会使用过多的RAM,或者在传输过程中使用过多的带宽。为了存储大于最大大小的文档,MongoDB提供了GridFS API。