mongodb创建一个在同一文档中具有多个值之和的字段

时间:2022-01-15 10:02:28

as the title say, i want to make a function in a document that sums all coins into TotalCoins for a specific user In mongodb . i found $add but i didnt know how to solve the problem with it.

正如标题所说,我想在一个文档中创建一个函数,该文档将所有硬币汇总为特定用户的TotalCoins在mongodb中。我发现$ add但我不知道如何解决它的问题。

PS:Im using Mongodb, mongoose nodejs express

PS:我使用Mongodb,mongoose nodejs表达

i have a User collection which has coins gained by this USER A and other coins with a percentage of other users that got reffered by this USER A.

我有一个用户集合,其中包含由此用户A获得的硬币和其他硬币,其中一部分硬币被此用户A所引用。

so the user schema looks like this

所以用户架构看起来像这样

{
    "_id": {
        "$oid": "5abcbee7ff1e4849b8f642b5"
    },
    "name": "test1",
    "email": "test1@gmail.com",
    "username": "test1",
    "password": "$2a$10$/Y6Gns8OPsp8eudVBrfUx.clwNwixAmeqhVzBYKUyNuoGcR3XQP8e",
    "joindate": "29/03/2018 11:24",
    "lastdailybonus": "28/03/2018 11:24",
    "profileimgurl": "img.jpg",
    "refferalUrl": "r1xk1rq5g",
    "refferedBy":"syiqyb9cz",
    "refferedUsers": [
        {
            "id": {
                "$oid": "5abcbf29ff1e4849b8f642b6"
            },
            "coins": 50
        },
        {
            "id": {
                "$oid": "5abcbf9b9a3ca4425489777e"
            },
            "coins": 76
        }
    ],
    "completedMissions": [],
    "coinsEarned":1500,
    "__v": 2
}
i want to have a new field TotalCoins which adds the sum of CoinsEarned and the coins in the refferedUsers Array (iterate through the array and sums the coins ) And this array can be exteneded when the Users reffer new friends. is it possible To achieve this operation ? it s complexe and if its impossible do you have any alternatives ? :) thanks for your time

UPDATE
Using aggregate: it s the same code you guys posted below but with some modification. unfortunatly, it doesnt work :/ the new totalCoins is not in the database yet.

更新使用聚合:它与你们在下面发布的代码相同,但有一些修改。不幸的是,它不起作用:/新的totalCoins还没有在数据库中。

var User = require('../models/users');

User.aggregate([{
            $addFields: {
                totalCoins: {
                    $reduce: {
                        input: "$refferedUsers",
                        initialValue: 0,
                        in: { $add : ["$$value", "$$this.coins"] }
                    }
                  }
                }
             },
             {$addFields:{totalCoins:{$add:["$totalCoins","$coinsEarned"]}}},
             {$out: "users"}
    ])

1 个解决方案

#1


0  

Try this:

尝试这个:

db.sample.aggregate([
{
    $addFields: {
        totalCoins: {
            $reduce: {
                input: "$refferedUsers",
                initialValue: 0,
                in: { $add : ["$$value", "$$this.coins"] }
            }
          }
        }
     },
     {$addFields:{totalCoins:{$add:["$totalCoins","$coinsEarned"]}}},
     {$out: "sample"}
])

#1


0  

Try this:

尝试这个:

db.sample.aggregate([
{
    $addFields: {
        totalCoins: {
            $reduce: {
                input: "$refferedUsers",
                initialValue: 0,
                in: { $add : ["$$value", "$$this.coins"] }
            }
          }
        }
     },
     {$addFields:{totalCoins:{$add:["$totalCoins","$coinsEarned"]}}},
     {$out: "sample"}
])