【问题标题】:How to copy field data and use it in another field of same document using mongoose?如何使用猫鼬复制字段数据并在同一文档的另一个字段中使用它?
【发布时间】:2022-01-03 12:37:13
【问题描述】:

我的 User Scehma 中有两个字段,购物车和订单都是数组。

我想将购物车数组中的所有商品复制到订单数组中,然后删除/删除购物车数组中的所有商品。我该怎么做?

【问题讨论】:

    标签: mongodb mongoose mongodb-query mongoose-schema


    【解决方案1】:

    使用更新$set$concatArrays

    db.collection.update({},
    [
      {
        $set: {
          orders: {
            $concatArrays: [
              "$orders",
              "$carts"
            ]
          },
          carts: []
        }
      }
    ])
    

    mongoplayground

    【讨论】:

    • 选择那个特定文档的查询会进去,更新后的{},对吧?
    • 是的,你是对的
    猜你喜欢
    • 2021-10-17
    • 2017-02-10
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多