【问题标题】:Incrementing number in an object of mongoose array增加猫鼬数组对象中的数字
【发布时间】:2022-01-15 11:02:00
【问题描述】:

我正在尝试使用 mongoose 增加 MongoDB 中的数字。但是我在查询阶段就开始失败了。当我// @ts-ignore 时,它会静默流动,没有错误,但值不会更新。

它抛出的错误是: Type 'number' is not assignable to type 'undefined'

我的架构:

const CartSchema: Schema<Document<ICart>> = new Schema({
    clientID: { type: String, required: true },
    sourceID: { type: String, required: true },
    items: {
        id: { type: Types.ObjectId },
        amount: { type: Number },
        grindHow: { type: String, trim: true },
        grind: Boolean,
        package: { type: String, trim: true },
        price: { type: Number },
        productID: { type: String },
        productName: { type: String },
        totalProduct: { type: Number },
    },
    source: { type: String, required: true },
}, { collection: "carts", timestamps: true });

CartSchema.virtual('cartTotal').get(function() {
    // @ts-ignore
    const self: any = this;
    
    if(self.items && self.items.length) {
        return self.items.reduce((acc: BigNumber, current: any) => acc.plus(current.totalProduct), new BigNumber(0))
    } else {
        return 0
    }
})

我尝试实现的方式是:

const item = await CartModel.findOneAndUpdate({ sourceID: clientID, 'items.id': itemID }, { $inc: { 'items.$.amount': 1 }}).lean({ virtuals: true })

我也试过updateOne。我想也许它更灵活。

【问题讨论】:

    标签: node.js typescript mongodb mongoose positional-operator


    【解决方案1】:

    items 不是数组,它是一个对象。您应该使用items.amount 访问它,而NOT 使用items.$.amount 访问它。像这样更改您的查询:

    const item = await CartModel.findOneAndUpdate({ sourceID: clientID, 'items.id': itemID }, { $inc: { 'items.amount': 1 }}).lean({ virtuals: true })
    

    【讨论】:

    • 其实在我的场景中应该是一个数组。据我所知,您只应该用对象表示法描述数组中的值。所以,Cannot create field 'amount' in element {items: [ { id: ObjectId('61b3a904b1cae85deecf4144'), grind: false, grindHow: null, package: "250", price: 74, amount: 2, productID: "61ac2a1809f47a4cca6498d4", productName: "Кения 4 фильтра", totalProduct: 148 } ]} 这就是我尝试这样做时得到的结果:await CartModel.findOneAndUpdate({ sourceID: clientID, 'items.id': itemID }, { $inc: { 'items.amount': 1 }}).lean({ virtuals: true })
    • 但在您的架构中,items 被定义为对象而不是数组。
    【解决方案2】:

    问题出在两个地方。

    我的 Schema 的问题 - 我将其描述为一个对象(感谢 @Nenad Milosavlievic) 正确的 Schema 是(或接近正确的。欢迎提供有关 Schema 的任何提示):

    const CartSchema: Schema<Document<ICart>> = new Schema({
        clientID: { type: String, required: true },
        sourceID: { type: String, required: true },
        items: { type: Array },
        source: { type: String, required: true },
    }, { collection: "carts", timestamps: true });
    

    以及我查询该项目的方式的另一个问题。我应该将字符串 ID 转换为ObjectId。我错过了...

    应该是这样的(加上我需要返回更新后的值,所以我添加了一个选项{ new: true }):

    await CartModel.findOneAndUpdate({ sourceID: clientID, 'items.id': Types.ObjectId(itemID) }, { $inc: { 'items.$.amount': 1 }}, { new: true }).lean({ virtuals: true })
    

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 2021-05-06
      • 2019-09-14
      • 1970-01-01
      • 2021-11-17
      • 2014-11-25
      • 2019-10-30
      • 1970-01-01
      • 2019-03-28
      相关资源
      最近更新 更多