Created
January 28, 2013 17:36
-
-
Save ynonp/4657446 to your computer and use it in GitHub Desktop.
mongoose relationships
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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