mongodb 具有子文档的用户与具有 UserId 的文档

mongodb User with subdocument vs document with UserId

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

Users/:userId/articles

Users/me/articles

问题一 我应该坚持使用文章模型还是应该将文章作为用户的子文档。例如

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 函数,还是必须制作自定义函数?

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