Node.js Mongoose更新父文档添加现有子文档id
我有两个猫鼬模型。类别和子类别 这些模型是使用meanjs.org中的登录自动加载的Node.js Mongoose更新父文档添加现有子文档id,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有两个猫鼬模型。类别和子类别 这些模型是使用meanjs.org中的登录自动加载的 category.model.js如下 ... const Schema = mongoose.Schema; const CategorySchema = new Schema({ name: { type: String, index: { unique: true, sparse: true }, validate: [validatePro
category.model.js
如下
...
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
name: {
type: String,
index: {
unique: true,
sparse: true
},
validate: [validateProperty, 'Please enter Category Name !']
},
description: {
type: String,
trim: true
},
subCatagories: [
{
type: Schema.Types.ObjectId,
ref: 'SubCategory'
}
],
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: [
'active', 'inactive', 'removed'
],
default: 'active'
}
});
...
mongoose.model('Category', CategorySchema);
...
const Schema = mongoose.Schema;
const SubCategorySchema = new Schema({
name: {
type: String,
index: {
unique: true,
sparse: true // For this to work on a previously indexed field, the index must be dropped & the application restarted.
},
validate: [validateProperty, 'Please enter SubCategory Name !']
},
description: {
type: String,
trim: true
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: [
'active', 'inactive', 'removed'
],
default: 'active'
}
});
...
mongoose.model('SubCategory', SubCategorySchema);
和subcategory.model.js
如下
...
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
name: {
type: String,
index: {
unique: true,
sparse: true
},
validate: [validateProperty, 'Please enter Category Name !']
},
description: {
type: String,
trim: true
},
subCatagories: [
{
type: Schema.Types.ObjectId,
ref: 'SubCategory'
}
],
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: [
'active', 'inactive', 'removed'
],
default: 'active'
}
});
...
mongoose.model('Category', CategorySchema);
...
const Schema = mongoose.Schema;
const SubCategorySchema = new Schema({
name: {
type: String,
index: {
unique: true,
sparse: true // For this to work on a previously indexed field, the index must be dropped & the application restarted.
},
validate: [validateProperty, 'Please enter SubCategory Name !']
},
description: {
type: String,
trim: true
},
updated: {
type: Date
},
created: {
type: Date,
default: Date.now
},
status: {
type: String,
enum: [
'active', 'inactive', 'removed'
],
default: 'active'
}
});
...
mongoose.model('SubCategory', SubCategorySchema);
通常情况下,子类别和类别将分别添加,稍后我需要将子类别分配给类别
我的分类更新方法如下面提到的代码
...
categoryRouter.route('/:catId').put((req,res)=>{
let category = req.category;
category = _.extend(category, _.pick(req.body, whitelistedFieldsForUpdate));
category.updated = Date.now();
category.save(function(err) {
if (err) {
return res.status(422).json({message: err});
}
res.json(category);
});
});
...
类别创建路径如下所示
categoryRouter.route('/').post((req, res) => {
req.body = _.pick(req.body, whitelistedFieldsForCreate);
let category = new Category(req.body);
category.save().then((data) => {
if (!data) {
res.status(422).json({message: "An error occured saving Category!"});
}
res.status(200).json(data);
}).catch((err) => {
res.status(422).json(err);
})
}));
首先,我发布到我的子类别结束点,并创建了一些子类别。然后我发布到类别,没有子类别,也创建了类别对象。现在我需要将(更新)类别对象及其子类别ID放入子类别数组中
当我发送包含对象id为
592e9878ee00dd962deb6e2e
的子类别的put请求时,数据库中存在的子类别不会更新。将put请求发送到何处?您正在尝试更新哪个集合?类别还是子类别?如果子类别是被引用的集合,则应对其进行更新,因为它是对该数据的更改。创建类别时,我将子类别数组保持为空。提到了类别的put请求代码。当我向具有子类别id的类别发送put请求时,我需要将该子类别添加到类别模型的子类别数组中。我们讨论的是“创建”应该是帖子,还是“更新”应该是“put”。如果您“创建”,那么您将保存在两个集合中,对吗?子类别中的新条目和类别中的\u id
引用。然而,当您“更新”时,您只是在谈论子类别。因此,在那里进行了更改,\u id
是相同的,因此在类别中没有任何更改。我基本上猜你是在尝试从类别中访问。这是错误的方法。使用子类别来放置“更新”似乎我的英语不好。很抱歉。首先,我发布到我的子类别结束点,并创建了一些子类别。然后我发布到类别,没有子类别,也创建了类别对象。现在我需要将(更新)类别对象及其子类别ID放入子类别数组中。我很清楚这一点,在你的问题中显示你正在使用的实际代码。我的观点是,听起来你这样做是错误的。如果你真的发布了代码,我们至少可以更正它。