Node.js Mongoose$inc updateMany不能使用非数字参数递增

Node.js Mongoose$inc updateMany不能使用非数字参数递增,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,节点API,其中我们有Mongo配置文件集合,每个配置文件都有订阅计划,表示他们为使用应用程序付费的剩余时间。 现在,我在后端的一部分工作,这将使所有配置文件的订阅计划减少1。 问题是,subscription\u plan被声明为String,所以我不能将其减少1 在这里得到一些提示后,我尝试了以下方法: router.put('/reduceSubscription', async (req, res) => { try { const updatedProfi

节点API,其中我们有Mongo配置文件集合,每个配置文件都有订阅计划,表示他们为使用应用程序付费的剩余时间。 现在,我在后端的一部分工作,这将使所有配置文件的订阅计划减少1。 问题是,subscription\u plan被声明为String,所以我不能将其减少1 在这里得到一些提示后,我尝试了以下方法:

router.put('/reduceSubscription', async (req, res) => {
    try {
        const updatedProfiles = await Profile.updateMany({ is_active: true }, [
            {
                $set: {
                    subscription_plan: {
                        $toString: {
                            $subtract: [{ $toInt: '$subscription_plan' }, 1]
                        }
                    }
                }
            }
        ]).exec();
        console.log(updatedProfiles);
    } catch (err) {
        res.status(500).send({ msg: err.message });
    }
});
在《邮递员》中进行测试后,我得到一条信息:

"msg": "Failed to parse number 'NaN' in $convert with no onError value: Bad digit \"N\" while parsing NaN" 
我将感谢任何帮助或代码片段,将帮助我解决这个问题。 谢谢:)

当前模式下无法执行您试图执行的操作(如错误所述),因为您的
配置文件
模型模式将
订阅计划定义为
字符串
,并且
$inc
仅适用于数字字段

更改该字段的架构类型,或通过创建具有一组聚合管道操作的管道来更新值,即1)获取当前值,2)使用将其转换为数值,3)使用递减值,4)使用将新值设置回字符串:


我尝试了你们的代码片段,“邮递员不要抛出错误”,它发送更新的json配置文件,但当我查看前端时,数据库中的值仍然是sameLet us。
router.put('/reduceSubscription', async (req, res) => {
    try {
        await Profile.updateMany(
            { is_active: true },
            [
                { '$set': {
                    'subscription_plan': {
                        '$toString': {
                            '$subtract': [
                                { '$toInt': '$subscription_plan' },
                                1
                            ]
                        }
                    }
                } }
            ]
        ).exec();
        res.json('Profiles updated');
    } catch (err) {
        res.status(500).send({ msg: err.message });
    }
});