2016-12-29 101 views
0

我有2种模式:猫鼬:找到所有参考文件

var pollSchema = new mongoose.Schema({ 
    title: String,  
    created: { 
     type: Date, default: Date.now 
    }, 
    options: [{ 
     label: String, 
     count: { 
      type: Number, default: 0 
     }, 
     backgroundColor: { 
      type: String, default: '#fff' 
     } 
    }], 
    author:{ 
     id:{ 
      type: mongoose.Schema.Types.ObjectId, 
      ref: "User" 
     },    
     username: String   
    } 
}); 



var userSchema = new Schema({ 
    username: {type: String, unique:true}, 
    email: {type: String, unique:true, lowercase: true}, 
    password: String 
}); 

现在每个调查都会存储它的作者的数据。 问题:

  • 如何重新设计我的模式 - 所以我将能够找到属于特定用户的所有投票?
  • 或者我应该让模式保持不变并找到另一种方法?

回答

1

你仍然可以找到所有属于一个特定的用户投票。你有这个author.id。你

还可以保持一个数组作为var userSchema = new Schema({ username: {type: String, unique:true}, email: {type: String, unique:true, lowercase: true}, password: String, polls: [] });

每一次用户调查,推民意调查数组,以后可以填充或获取计数里面的用户ID。