关于mongooseJs中MongoDb模式的困惑。怎么改进呢?

时间:2012-11-22 21:05:20

标签: mongodb mongoose

我这里有两份文件。

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
        }
    });

UserTwitterSchema:

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,并且已经使用了所有其他模式。只是想要第二个意见,因为这与我创建Relational DB的方式完全不同。

1 个答案:

答案 0 :(得分:0)

如果您计划接受具有其他凭据的用户(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)
 ...
}
相关问题