Mongoose无法从node.js中的数组中提取id
我的模式是这样的Mongoose无法从node.js中的数组中提取id,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我的模式是这样的 var PcardSchema = new Schema({ Comments:[{ type: Schema.Types.ObjectId, ref: 'commentSystem' }] }); module.exports=mongoose.model('Pcard',PcardSchema) 此时,我可以通过该中间件删除与该注释相关联的所有答案,但无法从PcardSchema中提取该注释id async.waterf
var PcardSchema = new Schema({
Comments:[{
type: Schema.Types.ObjectId,
ref: 'commentSystem'
}]
});
module.exports=mongoose.model('Pcard',PcardSchema)
此时,我可以通过该中间件删除与该注释相关联的所有答案,但无法从PcardSchema中提取该注释id
async.waterfall([
function (callback) {
commentResults.remove();//deleting all asnwer
callback(null);
},
function (callback) {
commentSystem.findByIdAndRemove(req.params.QuesId,function (err , comm) {
if(err){
callback(err);
}else{
callback(null); //here i am deleting the comment and getting that comment id
}
})
},
function (callback) {
**Pcard.update({ _id: req.params.PcardId},
{$pull:{'Comments': {'_id' : req.params.QuesID}}},function (err,rowaffected) {
if(err){** //this area of code is not working
callback(err);
}else{
callback(null);
}
}
)
}
],
是否可以从前置中间件中的注释数组中提取id?
有什么改进代码的建议吗 因为它只是
{“$pull”:{“Comments”:req.params.QuesID}
。对于“引用”设计,它只是一个“ObjectId数组”。您将其与“嵌入式文档数组”混淆,该数组中存在具有\u id
字段的文档对象。这不是您的模式中定义的。因此,感谢您的帮助。
async.waterfall([
function (callback) {
commentResults.remove();//deleting all asnwer
callback(null);
},
function (callback) {
commentSystem.findByIdAndRemove(req.params.QuesId,function (err , comm) {
if(err){
callback(err);
}else{
callback(null); //here i am deleting the comment and getting that comment id
}
})
},
function (callback) {
**Pcard.update({ _id: req.params.PcardId},
{$pull:{'Comments': {'_id' : req.params.QuesID}}},function (err,rowaffected) {
if(err){** //this area of code is not working
callback(err);
}else{
callback(null);
}
}
)
}
],