Felix
Felix

Reputation: 55

Mongodb, mongoose, Schema structure. get a collection into a field of other collection

I have a schema "Questions" it has like a dozen of questions in it, I can add and delete those questions, I need this collection reflected in a field of other collection - "User" with one additional field (nested in options).

Question Schema:

var QuestionScema = new mongoose.Schema({
  key: { type: String, required: true },
  label: { type: String, required: true },
  name: { type: String, required: true },
  page: { type: String, required: true },
  type: { type: String, required: true },
  options: [{ 
    key: {type: String, required: true}, 
    value: {type: String, required: true}
  }],
});

User Schema:

var UserSchema = new mongoose.Schema({
    Name: { type: String, required: true },
    Email: { type: String, required: true, unique: true },
    Password: { type: String, required: true },

    //this is where I need to reflect a Questions collection on each user, 
    //so that it will look something like this//

    Questions: [{
        key: {type: String, required: true},
        //here can be all other fields from Questions collection, that is not a problem
        options: [{ 
            key: {type: String, reuired: true},
            value: {type: String, reuired: true},
            counter: {type: Number, default: 0} //this is the additional field
        }]
    }],

    //

    Notifications: [{
        Title: { type: String },
        Data: { type: String },
        Created: { type: Date, default: Date.now }
    }]
});

I can't figure out how to do that. I have another collection of users, say User2 that will answer those questions from Questions collections and I need to keep track on Users schema (not User2, there I just save questions and answers) of how many times an option for that question is chosen.

A Questiuons entry can look like this:

  {
    key: Haveyouseenthismovie,
    label: Have you seen this movie?,
    name: Have you seen this movie?,
    page: 1,
    type: dropdown,
    options: [{ 
      key: yes, 
      value: yes
    }, { 
      key: no, 
      value: no
    }]
}

I want it to work like that (reflect a collection in field of each User) so I don't have to check if that question is in User collection if not add and if it is, is there an option that I need if it is than increment, if not than add that option (that user selected from options in that question in Questions schema) and increment. That looks like a bummer. So I figured that it will be better if that field will reflect a collection and I will just increment the option that I need on a question that I need. Please help me figure that out, I don't have enough practise in mongo so I struggle with it sometimes :)

Upvotes: 0

Views: 868

Answers (1)

Jay
Jay

Reputation: 24905

I don't think there is a way to reflect a collection in another document as the way you seem to wish it.

As I understand, the following options are available for you:

  • Embed the entire question document inside the User documents in User Collection.
  • Just maintain the '_id' of the question document in the User document in User Collection.

Please read on Data Modelling concepts & maintaining relationship between documents from Mongo DB Page https://docs.mongodb.com/manual/applications/data-models-relationships/

Upvotes: 1

Related Questions