Huy Tran
Huy Tran

Reputation: 4431

query with mongoose

this is my structure folder
-- express_example
|---- app.js
|---- models
|-------- songs.js
|-------- albums.js
|---- and another files of expressjs

song.js

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var SongSchema = new Schema({
name: {type: String, default: 'songname'}
, link: {type: String, default: './data/train.mp3'}
, date: {type: Date, default: Date.now()}
, position: {type: Number, default: 0}
, weekOnChart: {type: Number, default: 0}
, listend: {type: Number, default: 0}
});

mongoose.model('Song', SongSchema);
module.exports = SongSchema;


album.js

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, SongSchema = require('./songs')
, ObjectId = Schema.ObjectId;

var AlbumSchema = new Schema({
name: {type: String, default: 'songname'}
, thumbnail: {type:String, default: './public/images/album/unghoangphuc/U1.jpg'}
, date: {type: Date, default: Date.now()}
, songs: [SongSchema]
});

mongoose.model('Album', AlbumSchema);


How could i put code query album by album id in file album.js

Upvotes: 1

Views: 2083

Answers (1)

qiao
qiao

Reputation: 18219

example:

var mongoose = require('mongoose')
  , Album = mongoose.model('Album'); 

app.get('/posts/:id', function(req, res, next) {
  Album.findById(req.params.id, function(err, album) {
    // album is available here
  });      
});

see http://mongoosejs.com/docs/finding-documents.html to learn more about finding docs.

PS: this is the third time that I answered your question :)

Upvotes: 2

Related Questions