与mongoose查询
这是我的结构文件夹
– express_example
| —- app.js
| —-模型
| ——– songs.js
| ——– albums.js
| —-和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);
我怎么能把相册ID代码查询专辑放在album.js
文件中
例:
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 }); });
请参阅http://mongoosejs.com/docs/finding-documents.html以了解有关查找文档的更多信息。
PS:这是我第三次回答你的问题:)
- Express.js:POST数据作为req.body对象的KEY而不是req.body的VALUE?
- 如何parsingNodeJS服务器中由ajax发送的数组/对象
- 在Express / Node服务器上通过HTTP POST接收wavaudio文件
- 如何使用Node.js将JSON数组转换为CSV?
- 从Node / Express发送确认提醒到浏览器,无需重新载入页面
- 使用Node.js和Express.js删除MySQL中给定数组值的多行
- 有没有一种很好的方式使用EJS组队避免重复使用Express服务器?
- Node.js公共静态文件夹以UTF-8字符集服务js
- 在express.js上使用holder.js与express web框架一起工作