简体   繁体   English

如何在猫鼬中将子文档数组的特定元素标记为已修改?

[英]How do I mark a specific element of a subdocument array as modified in mongoose?

I have a Mongoose schema that looks like this: 我有一个如下所示的猫鼬模式:

let ChildSchema = new Schema({
    name:String
});

ChildSchema.pre('save', function(next){
    if(this.isNew) /*this stuff is triggered on creation properly */;
    if(this.isModified) /* I want to trigger this when the parent's name changes */;
    next();
});

let ParentSchema = new Schema({
    name: String,
    children: [ChildSchema]

});

The isNew stuff works as expected, but I want to mark the actual array elements of children as modified so the isModified stuff is triggered anytime the parent's name changes. isNew东西可以按预期工作,但是我想将children元素的实际数组元素标记为已修改,因此只要父级名称更改,就会触发isModified东西。 I'm not sure how to do this. 我不确定该怎么做。

I've tried: 我试过了:

ParentModel.findById(id)
    .then( (parentDocument) => {
        parentDocument.name = 'mommy'; //or whatever, as long as its different.
        if(parentDocument.isModified('name')){
            //this stuff is executed so I am detecting the name change.
            parentDocument.markModified('children');//probably works but doesn't trigger isModified on the actual child elements in the array
            for(let i=0; i < parentDocument.children.length; i++){
                parentDocument.markModified('children.'+i);//tried this as I thought this was how you path to a specific array element, but it has no effect.
            }
            parentDocument.save();//this works fine, but the child elements don't have their isModified code executed in the pre 'save' middleware
        }
    });

So my question - how do you mark specific (or all) array elements of a subdocument as modified such that their isModified property will be true? 因此,我的问题是-如何将子文档的特定(或所有)数组元素标记为已修改,以便其isModified属性为true? Note that my pre save middleware is getting executed fine, it but none of the items have isModified === true ; 请注意,我的预保存中间件可以很好地执行,但是没有任何项目具有isModified === true ;

Turns out the markModified method is available on the child itself (although since I'm using TypeScript I was mislead by the typing information given). 事实证明,孩子本身可以使用markModified方法(尽管由于我使用的是TypeScript,所以我对所提供的键入信息误导了)。

So if I do this: 因此,如果我这样做:

ParentModel.findById(id)
    .then( (parentDocument) => {
        parentDocument.name = 'mommy'; //or whatever, as long as its different.
        if(parentDocument.isModified('name')){
            for(let child of parentDocument.children){
                child['markModified']('name');
            }
            parentDocument.save();
        }
    });

It works. 有用。 Note that if I try to just mark the child itself as modified like this: 请注意,如果我尝试仅将子项本身标记为已修改,如下所示:

child['markModified']();

I get the error: 我收到错误:

MongoError: cannot use the part (children of children.{ name: 'theName'}) to traverse the element <bla bla bla>

I have no idea why that is the case, but it doesn't matter, in my case having some specific field marked as modified is fine. 我不知道为什么会这样,但是没关系,就我而言,将某些特定字段标记为“修改”是可以的。 Would be nice to know why the traverse error comes up, but my problem is fixed. 很高兴知道为什么会出现traverse错误,但是我的问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM