ObjectId 的 Mongoose Schema 默认值
Mongoose Schema default value for ObjectId
我有 collection 的仓库升级。它是预定义的 "template" collection 包含例如 max_capacity、级别和价格。然后我有 warehouse_levels collection,它包含不同的索引 warehouse_upgrades 用于不同的存储资源。但是我无法创建 warehouse_level 模型,因为我需要加载 warehouse_upgrades
的 _id
WarehouseUpgrade = mongoose.model("warehouse_upgrade");
// find wheat upgrade containing level 0
WarehouseUpgrade.find({ type: "wheat", level: 0 }).exec(function (err, wheat) {
var warehouseLevelSchema = Schema({
wheat: {
type: Schema.Types.ObjectId,
ref: "warehouse_upgrade",
default: wheat._id
},
... more resources
};
var WarehouseLevel = mongoose.model("warehouse_level", warehouseLevelSchema);
}
当我想调用时 var WarehouseLevel = mongoose.model("warehouse_level");解释此代码会引发错误:
MissingSchemaError: Schema hasn't been registered for model "warehouse_level"
如果我从 WarehouseUpgrade.find 中提取模式定义,那么代码可以工作,但我无法为资源仓库设置默认值。
当我不想对这些值进行硬编码时,如何从不同的 collection 设置 ObjectId 的默认值?
编辑:
我将所有模式定义加载到名为 mongoose.js:
的文件中
var mongoose = require("mongoose"),
Animal = require("../models/Animal");
Warehouse_upgrade = require("../models/Warehouse_upgrade"),
Warehouse_level = require("../models/Warehouse_level"),
User = require("../models/User"),
...
module.exports = function(config) {
mongoose.connect(config.db);
var db = mongoose.connection;
// And now I call methods for creating my "templates"
Warehouse_upgrade.createUpgrades();
Animal.createAnimals();
User.createDefaultUser();
}
MissingSchemaError 发生在 model/User(username, hashed_password, email, warehouse_level,...) - 每个用户都在 warehouse_level 中引用了自己的文档。
// User
var mongoose = require("mongoose"),
Schema = mongoose.Schema,
Warehouse_level = mongoose.model("warehouse_level");
// There are no users in DB, we need create default ones
// But first, we need to create collection document for warehouse_level
// and warehouse (not shown in this code snippet)
Warehouse_level.create({}, function (err, warehouseLevel) {
if (err) { console.error(err); return; }
// warehouse_level document is created, let's create user
User.create({ username: ..., warehouse_level: warehouseLevel._id });
});
实现此目的的一种可能方法是创建类似 "setDefaultIndexes"
的方法
var warehouseLevelSchema = mongoose.Schema({..});
warehouseLevelSchema.methods = {
setDefaultUpgrades: function() {
var self = this;
WarehouseUpgrade.find({ level: 0 }).exec(function (err, collection) {
for (var i = 0; i < collection.length; i++) {
var upgrade = collection[i];
self[upgrade.type] = upgrade._id;
}
self.save();
});
}
};
var Warehouse_level = mongoose.model("warehouse_level", warehouseLevelSchema);
并在创建新的 warehouse_level:
后调用它
WarehouseLevel.create({}, function (err, warehouseLevel) {
if (err) { console.error(err); return; }
warehouseLevel.setDefaultUpgrades();
});
我有 collection 的仓库升级。它是预定义的 "template" collection 包含例如 max_capacity、级别和价格。然后我有 warehouse_levels collection,它包含不同的索引 warehouse_upgrades 用于不同的存储资源。但是我无法创建 warehouse_level 模型,因为我需要加载 warehouse_upgrades
的 _idWarehouseUpgrade = mongoose.model("warehouse_upgrade");
// find wheat upgrade containing level 0
WarehouseUpgrade.find({ type: "wheat", level: 0 }).exec(function (err, wheat) {
var warehouseLevelSchema = Schema({
wheat: {
type: Schema.Types.ObjectId,
ref: "warehouse_upgrade",
default: wheat._id
},
... more resources
};
var WarehouseLevel = mongoose.model("warehouse_level", warehouseLevelSchema);
}
当我想调用时 var WarehouseLevel = mongoose.model("warehouse_level");解释此代码会引发错误:
MissingSchemaError: Schema hasn't been registered for model "warehouse_level"
如果我从 WarehouseUpgrade.find 中提取模式定义,那么代码可以工作,但我无法为资源仓库设置默认值。
当我不想对这些值进行硬编码时,如何从不同的 collection 设置 ObjectId 的默认值?
编辑: 我将所有模式定义加载到名为 mongoose.js:
的文件中var mongoose = require("mongoose"),
Animal = require("../models/Animal");
Warehouse_upgrade = require("../models/Warehouse_upgrade"),
Warehouse_level = require("../models/Warehouse_level"),
User = require("../models/User"),
...
module.exports = function(config) {
mongoose.connect(config.db);
var db = mongoose.connection;
// And now I call methods for creating my "templates"
Warehouse_upgrade.createUpgrades();
Animal.createAnimals();
User.createDefaultUser();
}
MissingSchemaError 发生在 model/User(username, hashed_password, email, warehouse_level,...) - 每个用户都在 warehouse_level 中引用了自己的文档。
// User
var mongoose = require("mongoose"),
Schema = mongoose.Schema,
Warehouse_level = mongoose.model("warehouse_level");
// There are no users in DB, we need create default ones
// But first, we need to create collection document for warehouse_level
// and warehouse (not shown in this code snippet)
Warehouse_level.create({}, function (err, warehouseLevel) {
if (err) { console.error(err); return; }
// warehouse_level document is created, let's create user
User.create({ username: ..., warehouse_level: warehouseLevel._id });
});
实现此目的的一种可能方法是创建类似 "setDefaultIndexes"
的方法var warehouseLevelSchema = mongoose.Schema({..});
warehouseLevelSchema.methods = {
setDefaultUpgrades: function() {
var self = this;
WarehouseUpgrade.find({ level: 0 }).exec(function (err, collection) {
for (var i = 0; i < collection.length; i++) {
var upgrade = collection[i];
self[upgrade.type] = upgrade._id;
}
self.save();
});
}
};
var Warehouse_level = mongoose.model("warehouse_level", warehouseLevelSchema);
并在创建新的 warehouse_level:
后调用它WarehouseLevel.create({}, function (err, warehouseLevel) {
if (err) { console.error(err); return; }
warehouseLevel.setDefaultUpgrades();
});