用猫鼬查询 [英] query with mongoose

查看:64
本文介绍了用猫鼬查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的结构文件夹
-express_example
| ---- app.js
| ----型号
| -------- songs.js
| -------- albums.js
| ----和expressjs的另一个文件

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);


如何将按专辑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 以了解有关查找文档的更多信息.

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

PS:这是我第三次回答您的问题:)

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

这篇关于用猫鼬查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆