Skip to content

Schema fields' naming convention for mongoose ODM

fredchu edited this page May 15, 2012 · 1 revision

Schema fields' naming convention for mongoose ODM

Boolean typed fields should have is_ prefix

// for example
PostSchema = new Schema({
  title        : { type : String },
  content      : { type : String },
  is_writable  : { type : String },
  is_published : { type : Boolean }
});

Fields that specifies a structure or relation

  • Fields to embed structural documents
// for example
var PostSchema = new Schema({
  title    : { type : String },
  content  : { type : String },
  user     : UserSchema,
  comments : [ CommentSchema ]
});
  • Fields to populate related objects
// for example
var PostSchema = new Schema({
  title    : { type : String },
  content  : { type : String },
  user     : { type : ObjectId },
  comments : [{ type : ObjectId}]
});
  • Fields to cache corresponding objects
// for example
var PostSchema = new Schema({
  title       : { type : String },
  content     : { type : String },
  as_user     : { type : Schema.Types.Mixed },
  as_comments : [{ type : Schema.Types.Mixed}]
});

Note that it seems that in mongoose _plural name to a corresponding model has special usage internally, so it is not a good idea to use _comments as the field name to refer to comments in a post.