Skip to content

Instantly share code, notes, and snippets.

@jwietelmann
Created November 26, 2012 17:34
Show Gist options
  • Save jwietelmann/4149554 to your computer and use it in GitHub Desktop.
Save jwietelmann/4149554 to your computer and use it in GitHub Desktop.
models.util = {
singleSubDoc: function(schema, virtualName) {
var realName = '_' + virtualName;
schema.virtual(virtualName)
.get(function() {
if(this[realName].length) return this[realName][0];
else return null;
})
.set(function(value) {
this[realName] = [value];
});
;
}
};
models.SchemaWithSingleLocation = new Schema({
_location: [Location],
});
models.util.singleSubDoc(models.SchemaWithSingleLocation, 'location');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment