Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬查询_Node.js_Mongodb_Mongoose_Express - Fatal编程技术网

Node.js 猫鼬查询

Node.js 猫鼬查询,node.js,mongodb,mongoose,express,Node.js,Mongodb,Mongoose,Express,这是我的结构文件夹 --表示示例 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'} ,

这是我的结构文件夹
--表示示例
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;
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);

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

如何在文件
album.js

中按相册id设置代码查询相册示例:

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;
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);
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
  });      
});
有关查找文档的详细信息,请参阅


附:这是我第三次回答你的问题:)

:)我三次得到你的回答,这对我很有用。我可以像函数一样将查询脚本放在文件
album.js
中吗?当我需要在单独的位置调用它时,可以使用该函数。对于简单的和内置的查询,不需要这样做,而且它通常由控制器而不是模型来处理。对于复杂查询,您可以向模型中添加新方法,请参见“确定”。。非常感谢你。我很高兴你对我的帮助。我会尽力帮助像你这样的人帮助我。