Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created January 28, 2013 17:36
Show Gist options
  • Save ynonp/4657446 to your computer and use it in GitHub Desktop.
Save ynonp/4657446 to your computer and use it in GitHub Desktop.
mongoose relationships
var mongoose = require('mongoose');
mongoose.connect('localhost/test');
var Schema = mongoose.Schema;
var AlbumSchema = new Schema({
artist: { type: Schema.Types.ObjectId, ref: 'Artist' },
name: String,
year: Number,
tracks: [String]
});
var ArtistSchema = new Schema({
name: String
});
var Artist = mongoose.model('Artist', ArtistSchema );
var Album = mongoose.model('Album', AlbumSchema );
var pink_floyd = new Artist({name: 'Pink Floyd'});
var darkside = new Album({
name: 'Dark Side Of The Moon',
year: 1973,
tracks: ['Speak To Me', 'Breathe In The Air'],
artist: pink_floyd
});
pink_floyd.save();
darkside.save();
mongoose.disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment