【问题标题】:How to use a specific collection Mongoose-Express-Mongodb (MEAN STACK)如何使用特定集合 Mongoose-Express-Mongodb (MEAN STACK)
【发布时间】:2014-06-05 06:22:36
【问题描述】:

我开始使用 MEAN STACK,所以我接受了他们的项目(关于发布文章),并且我正在尝试对其进行定制,以便获取我可以使用 angularjs 和 findOne 过滤的所有流的列表按身份证。 我遵循与他们为文章所做的相同的事情来创建与流相关的 JS 文件(流是我的对象)。所以我有一个名为流的集合,我将它导入到 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);
        }
    });
};

我也添加了路由...但是它不起作用,你认为我犯了一些错误吗?提前感谢您的帮助

【问题讨论】:

  • Express 中间件总是采用三个参数:req、res 和 next。您的流程方法中的 ID 为“未定义”。根据您实现路由的方式,您很可能会在 req.params 或 req.body 中找到它。

标签: node.js mongodb express mongoose mean-stack


【解决方案1】:

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

您可以在创建 Schema 时显式选择一个集合:

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

或者您可以稍后在创建的架构上设置它:

// Flow Schema
var FlowSchema = new Schema({
    /* attributes...*/
});

FlowSchema.set('collection', 'my-collection');

我相信这是在 Mongoose 3.4 中添加的,因此请检查您正在使用的 Mongoose 的版本。

【讨论】:

    【解决方案2】:

    有关强制集合名称的查询,请参阅此帖子:

    Mongoose -- Force collection name

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

    qux 是您的集合,假设您在 mongo-connect 中连接到正确的数据库。

    【讨论】:

      猜你喜欢
      • 2017-08-30
      • 2016-12-11
      • 2015-08-25
      • 2014-12-23
      • 2021-07-10
      • 2016-04-27
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多