如何使用特定的集合Mongoose-Express-Mongodb(MEAN STACK)

我正在开始使用MEAN STACK,所以我把他们的项目(关于发表文章),我试图costomize它,以获得所有stream量,我可以用angularjs过滤,也可以通过id的findOne列表。 我遵循了他们为文章创build与stream相关的JS文件的相同的事情(stream是我的对象)。 所以我有一个名为stream的集合,我将其导入到MEAN STACK(db == mean-dev)所使用的相同数据库中,并试用了以下代码:

// myApp/serves/models/flow.js 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema; // Flow Schema var FlowSchema = new Schema({ _id: { type: Number, default: '' }, name: { type: String, default: '', trim: true }, Clients: { type: Array, default: '', trim: true }, DP Prot: { type: String, default: '' } /* And 15 others attributes...*/ }); /** Statics */ FlowSchema.statics.load = function(id, cb) { this.findOne({ _id: id }).exec(cb); }; // Define the collection mongoose.model('Flow', FlowSchema); 

和控制器代码:

 // servers/controllers/flows.js 'use strict'; /** * Module dependencies. */ var mongoose = require('mongoose'), Flow = mongoose.model('Flow'), _ = require('lodash'); /** * Find flow by id */ exports.flow = function(req, res, next, id) { Flow.load(id, function(err, flow) { if (err) return next(err); if (!flow) return next(new Error('Failed to load flow ' + id)); req.flow = flow; next(); }); }; /** * New code count Flows */ exports.compte = function(req, res) { var c; flow.count({}, function(err, count) { if (err) return next(err); c = count; res.jsonp (count); }); }; /** * Show Flow */ exports.show = function(req, res) { res.jsonp(req.flow); }; /** * List of Flows */ exports.all = function(req, res) { Flow.find().sort('-name').populate('name', 'application').exec(function(err, flows) { if (err) { res.render('error', { status: 500 }); } else { res.jsonp(flows); } }); }; 

我也添加了路线…但是这不起作用,你认为我犯了一些错误吗? 预先感谢您的帮助

该文档向您展示了如何: http : //mongoosejs.com/docs/guide.html#collection

创build架构时,您可以明确地select一个集合:

 // Flow Schema var FlowSchema = new Schema({ /* attributes...*/ }, { collection: 'my-collection' }); 

或者您可以稍后将其设置在创build的模式中:

 // Flow Schema var FlowSchema = new Schema({ /* attributes...*/ }); FlowSchema.set('collection', 'my-collection'); 

这是添加在mongoose3.4我相信,所以检查你使用的mongooseverison。

查看这个post查询强制收集名称:

mongoose – 力量集合名称

 var mySchema = new Schema({ foo: bar }, { collection: qux }); 

qux是你的集合,假设你连接到你的mongo连接的正确的数据库。