【问题标题】:Use arrayFilters in order to update a property of an object in nested array of objects, with Mongoose使用 arrayFilters 来更新嵌套对象数组中对象的属性,使用 Mongoose
【发布时间】:2022-09-23 04:53:59
【问题描述】:

我在 MongoDb 中有一个集合,其中包含包含对象字段数组的文档 \"flights\"。我想一次更新一个对象的一个​​属性。为此,我必须使用两个过滤器:一个用于选择我想要更新的文档,第二个用于选择数组中的对象。

我正在使用 arrayFilters 和 Mongoose,如下所示:

这是我的飞行shema

const mongoose = require(\'mongoose\')
const Schema = mongoose.Schema

const flightSchema = new Schema({
    flightName :{ type : String, required :true}, 
    sits : {type : Array, required : true}, //[{n:1, d:f, s:f}]
    origin : {type: String, required : true},
    destination : {type : String, required: true},
    departure : {type : Date, required : true},
    arrival : {type : Date, required : true}
})

module.exports = mongoose.model(\'Flight\', flightSchema)
// Models/Flight.js
{
    flightName: a164651,
    origin: Monterrey,
    detination: Cancun,
    sits: [{
        sitNumber: 1,
        isAvailable: true,
        isSuspended: false
    }, {
        sitNumber: 2,
        isAvailable: true,
        isSuspended: false
    }]
}

让我们想象一下,我想使用 sitNumber : 2 将对象中的 IsSuspended 属性从 false 更新为 true。

//Controllers dashboard.js

blockSit : async (req, res) => {
    try {
        const flight = req.body.flightName
            
        const sit = req.body.sitToBlock //sit es 2 
            
        const updateSit = await Flight.updateOne(
            { \"flightName\": flight},

            { \"$set\" : {\"sits.$[si].isSuspended\": true} }, 

            { \"arrayFilters\": [{ \"si.sitNumber\": sit} ]}
        )
                
        console.log(updateSit) 
    } catch (error) {
        console.log(error)
    }
}

据我所见,我的 sintaxis 是正确的。但是我不断收到以下错误消息:

Error: Could not find path \"sits.0.sitNumber\" in schema

我不必使用数组过滤器。我愿意尝试任何其他允许我使用 mongoose 更新嵌套对象数组中的属性的解决方案。

  • 您的 Mongoose 飞行模式是什么?
  • 我编辑了我的问题以添加我的架构。

标签: node.js mongoose


【解决方案1】:

看起来您的 sits 字段是子文档数组,但没有描述字段的模式。尝试定义架构。

const sitSchema = new Schema({
  sitNumber: Number,
  isAvailable: Boolean,
  isSuspended: Boolean,
  // etc
});

// in the flight schema

  sits: [sitSchema],

【讨论】:

    【解决方案2】:

    我就是这样解决的:

    首先我需要在 Mongoose 中使用 findOneandUpdate 方法。 另外,我向arrayFilters 添加了新属性并将其设置为true。

       blockSit : async (req, res) =>{
            try {
                const flight = req.body.flightName
                console.log(flight)
                const sit = req.body.sit
                console.log(sit)
                const updateSit = await Flight.findOneAndUpdate(
                    {"flightName" :  flight},
                    { "$set" : {"sits.$[si].isSuspended" : true, "sits.$[si].isAvailable": false} }, 
                    {"arrayFilters" :[ { "si.sitNumber" :sit} ], new : true}
    
                )
                
             
                    
                res.send(updateSit)
    
                
            } 
            catch (error) {
                console.log(error)
            }
        },
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      相关资源
      最近更新 更多