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似乎不起作用。
1条答案
按热度按时间bq8i3lrv1#
您可以使用聚合管道(docs)更新MongoDB文档。
下面是查询可能的样子:
下面是MongoDB playground link,查看查询工作。