【问题标题】:Mongoose delete other objects after updateMongoose 更新后删除其他对象
【发布时间】:2022-01-24 13:25:31
【问题描述】:

我在 mongoose 上的更新查询遇到问题。我不确定为什么在更新特定对象后会删除其他对象。代码在我更新时有效,但在那之后,数组中的其余对象将被删除/移除。从字面上看,所有剩余的对象在更新请求后都会被删除。

export const updateProduct = async (req,res) => {
     const { id } = req.params;
 
     try {
         if(!mongoose.Types.ObjectId.isValid(id)) return res.status(404).json({ message: 'Invalid ID' });
 
         await OwnerModels.findOneAndUpdate({'_id': id, store:{$elemMatch: {productname: req.body.store[0].productname }}},
             {$set: 
                 {
                     store: 
                         {
                             productname: req.body.store[0].productname,
                             price: req.body.store[0].price,
                             quantity: req.body.store[0].quantity,
                             categoryfilter: req.body.store[0].categoryfilter,
                             description: req.body.store[0].description,
                             timestamp: req.body.store[0].timestamp                        
                         }
                 }
         }, // list fields you like to change
         {'new': true, 'safe': true, 'upsert': true});
 
     } catch (error) {
         res.status(404).json(error)
     } }

【问题讨论】:

    标签: javascript node.js mongodb mongoose


    【解决方案1】:

    我不确定为什么在更新特定对象后会删除其他对象。

    因为您正在更新整个对象,它将替换数据库中现有的存储对象数组,

    你需要使用arraFilters,upsert在更新的对象数组中无效,所以我评论了,

    await OwnerModels.findOneAndUpdate(
        {
            '_id': id, 
            store:{
                $elemMatch: {
                    productname: req.body.store[0].productname 
                }
            }
        },
        {
            $set: {
                store: {
                    "store.$[s].productname": req.body.store[0].productname,
                    "store.$[s].price": req.body.store[0].price,
                    "store.$[s].quantity": req.body.store[0].quantity,
                    "store.$[s].categoryfilter": req.body.store[0].categoryfilter,
                    "store.$[s].description": req.body.store[0].description,
                    "store.$[s].timestamp": req.body.store[0].timestamp
                }
            }
        },
        {
            'arrayFilters': [
                { "s.productname": req.body.store[0].productname }
            ],
            'new': true, 
            'safe': true, 
            // 'upsert': true
        }
    );
    

    【讨论】:

    • 我尝试使用点符号。它给了我状态 404 错误..好吧,为什么。我正在检查 req.body 它似乎运行良好..
    • 请提供完整的错误堆栈,404 无助于理解问题。
    • MongoServerError: Cannot create field 'categoryfilter' in element {store: [ { productname: "monitor", price: 1500, quantity: 5, categoryfilter: "Monitors", description: "tests", timestamp : "2021 年 12 月 24 日下午 1:39:36", _id: ObjectId('61c55d687d8ff305de3ed044') } ]}
    • 我也尝试添加 $ 运算符,但错误仍然像这样 - MongoServerError: Cannot create field 'categoryfilter' in element {store: [ { productname: "monitor", price: 1500, quantity : 5, categoryfilter: "Monitors", description: "tests", timestamp: "December 24th 2021, 1:39:36 pm", _id: ObjectId('61c55d687d8ff305de3ed044') } ]}
    猜你喜欢
    • 2018-04-26
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2018-10-16
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多