简体   繁体   English

"$pull" 不删除 mongoose 数组的嵌入文档

[英]"$pull" doesn't delete the embedded documents of mongoose array

My mongo schema is as below:我的 mongo 架构如下:

resourceId: {
        type: String,
    },

    resourceName: {
        type: String,
    },

    dateAndValue: [
        {
            date: { type: Date },
            value: { type: Number },
        },
    ],

Once data is added to the "dateAndValue" array, I want to remove all objects in the array that includes '0' as the value.将数据添加到“dateAndValue”数组后,我想删除数组中包含“0”作为值的所有对象。 This is the code I used for it but it doesn't seem to work:这是我使用的代码,但它似乎不起作用:

await QuantumResourcesManpowerAdmin.updateMany(
    { resourceId: qrmaRecord.resourceId },
    {
        $pull: {
            dateAndValue: {
                $elemMatch: { value: 0 },
            },
        },
    },
    { multi: true }
);

Found the answer, $elemMatch should be removed from the above code.找到了答案,$elemMatch 应该从上面的代码中删除。 So,所以,

await QuantumResourcesManpowerAdmin.update(
    { resourceId: qrmaRecord.resourceId },
    {
        $pull: {
            dateAndValue: {
                value: 0,
            },
        },
    },
    { safe: true, multi: true }
);

would work without any issue可以正常工作

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

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