Skip to content

Instantly share code, notes, and snippets.

@krevels
Created October 18, 2013 14:24
Show Gist options
  • Save krevels/7042315 to your computer and use it in GitHub Desktop.
Save krevels/7042315 to your computer and use it in GitHub Desktop.
Mongoose: toJSON() on detached entities
var FeedSchema = new Schema({
title: String,
description: String,
link: String,
rss: String,
pubdate: Date,
updated: { type: Date, default: Date.now },
items: [{type: Schema.Types.ObjectId, ref: 'Item'}],
});
var ItemSchema = new Schema({
title: String,
description: String,
link: String,
source: String,
guid: String,
author: String,
pubdate: Date,
updated: { type: Date, default: Date.now },
_feed: { type: Schema.Types.ObjectId, ref: 'Feed' }
});
mongoose.model('Feed', FeedSchema);
mongoose.model('Item', ItemSchema);
-------------
var Feed = mongoose.model('Feed');
var feed = new Feed();
feed.items.push(new Item());
console.log(feed.toJSON());
{
"title": "BBC News - Home",
"description": "The latest stories from the Home section of the BBC News web site.",
"pubdate": "2013-10-18T01:30:10.000Z",
"link": "http://www.bbc.co.uk/news/#sa-ns_mchannel=rss&ns_source=PublicRSS20-sa",
"_id": "52609041036001b379000052",
"items": [
"52609041036001b379000053"
],
"updated": "2013-10-18T01:34:57.167Z",
"created": "2013-10-18T01:34:57.000Z",
"id": "52609041036001b379000052"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment