【问题标题】:Mongoose: find all referenced documentsMongoose:查找所有引用的文档
【发布时间】:2017-05-13 11:15:23
【问题描述】:

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

现在每个民意调查都将存储其作者的数据。 问题:

  • 如何重新设计我的架构 - 这样我才能找到属于特定用户的所有民意调查?
  • 还是应该让架构保持不变并寻找其他方法?

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    您仍然可以找到属于特定用户的所有民意调查。你有 author.id 。

    您也可以将数组保留为var userSchema = new Schema({ username: {type: String, unique:true}, email: {type: String, unique:true, lowercase: true}, password: String, polls: [] });

    每次用户投票时,将 userId 推送到 polls 数组中,您可以稍后填充或获取计数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-10
      • 2016-04-23
      • 2014-08-23
      • 2016-01-19
      • 1970-01-01
      • 2017-02-28
      • 2023-03-20
      • 2019-06-05
      相关资源
      最近更新 更多