mongodb 如何用数组元素中的另一个字段更新数组元素中的一个字段?

ee7vknir  于 2023-03-29  发布在  Go
关注(0)|答案(1)|浏览(134)
import mongoose from 'mongoose'

const holderSchema = new mongoose.Schema(
    {
        walletId: {
            type: String,
            required: true,
            unique: true,
        },
        solBalance: {
            type: Number,
            default: 0,
            min: 0,
        },
        liquidityPools: [
            {
                collectionId: {
                    type: mongoose.Schema.Types.ObjectId,
                    required: true,
                },
                lpTokens: {
                    type: Number,
                    required: true,
                    min: 0,
                },
                feeEarned: {
                    type: Number,
                    default: 0,
                    min: 0,
                },
                isActive: {
                    type: Boolean,
                    default: true,
                },
            },
        ],
    },
    { timestamps: true }
)

export default mongoose.models.Holder || mongoose.model('Holder', holderSchema)

在这个模型中,我怎么能做这样的事情- liquidityPools.feeEarned += liquidityPools.lpTokens /100对于所有文档的数组元素,其中liquidityPools.collectionId为5

await Holder.updateMany(
                        { 'liquidityPools.collectionId': collectionId, 'liquidityPools.isActive': true },
                        {
                            $inc: {
                                'liquidityPools.$[elem].feeEarned': {
                                    $multiply: [
                                        'elem.lpTokens',100
                                    ],
                                },
                            },
                        }
                    )
                 })`

这是我尝试过的,但在$inc中使用$multiple似乎不起作用。

bq8i3lrv

bq8i3lrv1#

您可以使用聚合管道(docs)更新MongoDB文档。
下面是查询可能的样子:

db.collection.update({
  "liquidityPools.collectionId": "60d1a0a0fd92f81b8e7a1dc1",
  "liquidityPools.isActive": true
},
[
  {
    $set: {
      liquidityPools: {
        $map: {
          input: "$liquidityPools",
          as: "pool",
          in: {
            $cond: [
              {
                $eq: [
                  "$$pool.collectionId",
                  "60d1a0a0fd92f81b8e7a1dc1"
                ]
              },
              {
                collectionId: "$$pool.collectionId",
                lpTokens: "$$pool.lpTokens",
                feeEarned: {
                  $add: [
                    "$$pool.feeEarned",
                    {
                      $multiply: [
                        "$$pool.lpTokens",
                        100
                      ]
                    },
                    
                  ],
                  
                },
                isActive: "$$pool.isActive",
                
              },
              "$$pool",
              
            ],
            
          },
          
        },
        
      },
      
    },
    
  },
  
])

下面是MongoDB playground link,查看查询工作。

相关问题