mongodb update one with upset未按预期工作

t0ybt7op  于 2023-08-04  发布在  Go
关注(0)|答案(1)|浏览(103)

这是我的 Mongoose 模式。

const CurrencySchema = new mongoose.Schema({
    source: { type: String, uppercase: true, required: true, minlength: 3, maxlength: 3, trim: true },
    convert: { type: String, uppercase: true, required: true, minlength: 3, maxlength: 3, trim: true },
    exchange: [
        {
            year: { type: Number, required: true, min: [2000, 'Year can be only positive value'], },
            rate: { type: Number, min: [0, 'Exchange rate can be only positive value'], required: true }
        }
    ],
    created_by: { type: String, max: 100 },
    updated_by: { type: String, max: 100 }

}, { versionKey: false, timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' } });

字符串
我必须使用下列条件更新此集合。
我将发送源,转换,年,率在一个例行程序。
1.如果集合中的source和convert不可用,则应创建一个包含汇率和年份的文档
1.如果source和convert是可用的,但是year在交换列表中是不可用的,那么它应该将{year,rate}对象推入交换列表。
1.如果兑换列表中有年份,则应更新该年份的汇率。
我现有查询是,

const response = await Currencies.updateOne(
            { source, convert },
            [{
                $set: {
                    exchange: {
                        $cond: [
                            { $in: [year, "$exchange.year"] },
                            {
                                $map: {
                                    input: "$exchange",
                                    in: {
                                        $cond: [
                                            { $eq: ["$$this.year", year] },
                                            {
                                                year: "$$this.year",
                                                rate: rate
                                            },
                                            "$$this"
                                        ]
                                    }
                                }
                            },
                            { $concatArrays: ["$exchange", [{ year: year, rate: rate }]] }
                        ]
                    }
                }
            }],
            {
                upsert: true
            }
        )


当我尝试传递这个输入{source: EUR, convert: XCD, year: 2022, rate: 3.5}时,查询抛出$in requires an array as a second argument, found: missing错误。当数据库中的source和convert不可用时。
我怎么能解决这个与我的所有条件?

3phpmpom

3phpmpom1#

您可以检查某个字段是否不是数组,如果是,只需为该字段分配一些值。

const response = await Currencies.updateOne(
    { source, convert },
    [{
        $set: {
            exchange: {
                $cond: {
                    if: {$isArray: "$exchange"},
                    then: {
                        $cond: [
                            { $in: [year, "$exchange.year"] },
                            {
                                $map: {
                                    input: "$exchange",
                                    in: {
                                        $cond: [
                                            { $eq: ["$$this.year", year] },
                                            {
                                                year: "$$this.year",
                                                rate: rate
                                            },
                                            "$$this"
                                        ]
                                    }
                                }
                            },
                            { $concatArrays: ["$exchange", [{ year: year, rate: rate }]] }
                        ]
                    },
                    else: [{ year: year, rate: rate }]
                }
            }
        }
    }],
    {
        upsert: true
    }
)

字符串

相关问题