Mongoose - 更新文档,添加新键(有 strict:false)
Mongoose - Updating a document , adding new keys (having strict:false)
我有以下猫鼬模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var companySchema = new mongoose.Schema({
name: String,
long_name: String,
address: String,
telephone: String,
mobile_phone: String,
fax: String,
email: String,
url: String,
config:{
language: String,
account: String
},
items:[{
name: String,
internal_id: String,
reference_code: String,
description: String
}]
},{ timestamps: true, strict: false });
var Company = mongoose.model('Company', companySchema);
module.exports = Company;
好的,这个想法是让每个用户在项目数组中插入他们自己的字段。例如,用户将使用几个新的 key/value 对创建一个新项目。我会将新项目存储在项目数组中:
var Company = require('mongoose').model('Company');
exports.createItem = function(req, res, next) {
var newitem = {
"name": "Syringe Needels",
"internal_id": "ID00051",
"reference_code": "9506",
"description": "My description",
"batch": "100",
"room": "240",
"barcode": "9201548121136"
};
Company.findById(req.company, function(error, company) {
company.items.push(newitem);
company.save(function(err, doc){
return res.status(200).send('The new item has been stored!');
});
});
};
架构中未定义的键未被存储;仅存储名称、internal_id、reference_code 和描述。
如何实现?
您可以创建数据类型为混合对象的子文档。这将使您可以动态插入/更新密钥
var companySchema = new mongoose.Schema ({
name: String,
long_name: String,
address: String,
telephone: String,
mobile_phone: String,
otherProps : {}
})
这(otherProps 键)将完成任务
我有以下猫鼬模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var companySchema = new mongoose.Schema({
name: String,
long_name: String,
address: String,
telephone: String,
mobile_phone: String,
fax: String,
email: String,
url: String,
config:{
language: String,
account: String
},
items:[{
name: String,
internal_id: String,
reference_code: String,
description: String
}]
},{ timestamps: true, strict: false });
var Company = mongoose.model('Company', companySchema);
module.exports = Company;
好的,这个想法是让每个用户在项目数组中插入他们自己的字段。例如,用户将使用几个新的 key/value 对创建一个新项目。我会将新项目存储在项目数组中:
var Company = require('mongoose').model('Company');
exports.createItem = function(req, res, next) {
var newitem = {
"name": "Syringe Needels",
"internal_id": "ID00051",
"reference_code": "9506",
"description": "My description",
"batch": "100",
"room": "240",
"barcode": "9201548121136"
};
Company.findById(req.company, function(error, company) {
company.items.push(newitem);
company.save(function(err, doc){
return res.status(200).send('The new item has been stored!');
});
});
};
架构中未定义的键未被存储;仅存储名称、internal_id、reference_code 和描述。
如何实现?
您可以创建数据类型为混合对象的子文档。这将使您可以动态插入/更新密钥
var companySchema = new mongoose.Schema ({
name: String,
long_name: String,
address: String,
telephone: String,
mobile_phone: String,
otherProps : {}
})
这(otherProps 键)将完成任务