1

Working on my first MEAN.JS project, I have the following (simplified) subdoc'ed schema arrangement representing members of a group assigned to one another (for a Secret Santa website):

var MemberSchema = new Schema({
  displayName: { type: String },
  has: {
    type: Schema.ObjectId,
    ref: 'Member'
  }
});
mongoose.model('Member', MemberSchema);

var GroupSchema = new Schema({
  displayName: { type: String },
  members: [MemberSchema]
});
mongoose.model('Group', GroupSchema);

How should I best structure my server-side create and update controllers and the POST bodies they expect to receive? The Articles example uses succinct var article = new Article(req.body); and its implied POST body structure, but that will obviously fail since the members array will be encoded as a string and besides, wouldn't handle the Member -> Member references. How then should I encode my data in such a way as to facilitate easy serialization and integration with Angular binding client-side, especially in light of using subdocs with references to other subdocs?

As I see it, I could structure the POST body like:

displayName: 'My group'
members[0]: { _id: '81ds72', displayName: 'Member 1', has: '0cc9sk' }
members[1]: { _id: '0cc9sk', displayName: 'Member 2', has: '81ds72' }

or:

displayName: 'My group'
members[0]._id: '81ds72'
members[0].displayName: 'Member 1'
members[0].has: '0cc9sk'
members[1]._id: '0cc9sk'
members[1].displayName: 'Member 2'
members[1].has: '81ds72'

or I suppose even just as JSON as:

displayName: 'My group'
members: '[{"_id": "81ds72", "displayName": "Member 1", "has": "0cc9sk"}, {"_id": "0cc9sk", "displayName": "Member 2", "has": "81ds72"}]'

or all together as JSON:

data: '{"displayName": "My group", "members": [{"_id": "81ds72", "displayName": "Member 1", "has": "0cc9sk"}, {"_id": "0cc9sk", "displayName": "Member 2", "has": "81ds72"}]}'

Frankly, all of these seem just awful, so I'm wondering if perhaps I'm just approaching this the wrong way. I'd like to continue to use subdocs, since they really best represent the data I want to store, but it seems like a round peg to MEAN.JS's CRUD square hole.

0x24a537r9
  • 968
  • 1
  • 10
  • 24

0 Answers0