5

I am creating sort of a conversation app, I want a user to be able to continue their conversation from anywhere via email and SMS. Right now I have a conversation schema like this:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var validator = require('validator');

var ConversationSchema = new Schema({
    contact: [
        { 
            type: Schema.Types.ObjectId,
            ref: 'Contact'
        }
    ],
    conversation: {

    }
});

var Conversation = mongoose.model('Conversation', ConversationSchema);

In the conversation object, I want an Email or an SMS object to be able to be store. It will not be known how many Email's or how many SMS's will be in the conversation object, nor what order they arrive in. When I query for the conversation, to display it, I want to populate all of the Email and SMS objects.

How can I ensure conversation can hold different types of objects, and that each of those objects can be populated when the conversation is requested?

Is there a better way to accomplish this? I can imagine that populating every object would be extremely inefficient.

Trevor Hutto
  • 2,112
  • 4
  • 21
  • 29

1 Answers1

0

In your case, you'd better use an array for conversation, such as:

conversation: {
    sms: [],
    email: []
}

or,, if you put [Schema.Types.Mixed] for the type, you will be able to save any kind of object inside.