【问题标题】:Confusion regarding MongoDb schema in mongooseJs. How to improve it?关于 mongooseJs 中的 MongoDb 模式的困惑。如何改进它?
【发布时间】:2012-11-30 04:59:58
【问题描述】:

我这里有两份文件。

var UserSchema = new Schema({
        username: {
          type: String,
          validate: [required,"Username is required"],
          index: {unique: true}
        },
        email: {
            type: mongoose.SchemaTypes.Email,
            validate: [required, 'Email required'],
            index: { unique: true }
        },
        password: {
            type: String,
            validate: [required, 'Password required'],
        },
        socialauth:{
          type:[]
        },

        createdAt: {
            type: Date,
            'default': Date.now
        }
    });

用户TwitterSchema:

var UserTrwitterSchema = new Schema({

  authToken: {
      type: String,
      validate: [required,"authToken is required"],
      index: {unique: true}
    },

  authSecret: {
      type: String,
      validate: [required,"authSecret is required"],
      index: {unique: true}
    },
  apiKey: {
        type: String,
        validate: [required, 'Api Key is required'],
    }, 
});

我在这里的困惑是: 好吧,我应该使用 apiKey 还是使用 user :ObjectId, 来代替它。用户将是这里的外键。还是两者都做?在查询数据时,什么成本最低? 我在这里使用了 apiKey,并且对所有其他模式都做了同样的事情。只是想要第二个意见,因为这与我创建关系数据库的方式完全不同。

【问题讨论】:

  • 你在做什么查询?另外,UserTwitter 对 User 来说是一个单独的文档吗?
  • 你在做什么查询?

标签: mongodb mongoose


【解决方案1】:

如果您打算接受具有其他凭据的用户 (facebook....),我不会创建单独的 twitter 架构

相反,我会创建一个配置文件集合,其中类型和凭据作为唯一键,并将用户数据(年龄、生物等)存储在用户 ollection 中。

profile = {
 _id : {type : "email", username : "Justin"},
 creds : {email : "foo@bar.com", pw : "hash"),
 user_id : ObjectId(xys)
}

profile2 = {
 _id : {type : "twitter", username : "JustinBieber"},
 creds : {token : "fatoken", secret : "hash" , key : "key"),
 user_id : ObjectId(xys)

}

用户看起来像:

user = {
 _id : ObjectId(xys),
 bio : "really young guy",
 born : date(March 1, 1994)
 ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2020-03-06
    • 2016-03-16
    • 2013-07-27
    相关资源
    最近更新 更多