【发布时间】:2018-12-05 05:51:41
【问题描述】:
这让我很头疼。我想将集合名称传递给 Mongoose Schema 对象实例的模型构造函数。
let mongoose = require('mongoose'),
Schema = mongoose.Schema;
let schema = new Schema({
name: {type: String}
}, { collection: 'testing' });
module.exports = mongoose.model("Item", schema);
我可以使用上面提到的集合参数将它从项目更改为测试。我还尝试通过对象上的方法设置属性。它正确设置它,如在预保存挂钩中看到的那样。但该实例仍使用原始集合名称。我想从调用代码中传入一个集合名称。
let schema = require(path.join(__rel__, __models__, "schema"));
let item = new schema(itemObject);
item.save(function(err, item) {});
我想通过某种方式为要存储的对象传入一个新的集合名称。
【问题讨论】:
标签: javascript node.js mongodb mongoose mongoose-schema