Mongodb compare two fields with matched fields value aggregate

六眼飞鱼酱① 提交于 2021-01-28 09:00:58

问题


I am having problems in my query, The parameter I am passing are voucherId, and userId, I need to check if that user exceeded the limitedQuantityPerUser. If yes, exclude this voucher. But I am having problems comparing the matching fields, example if voucherId is 'aaa', check if the quantityBrought > limitedQuantityPerUser.

I have tried this

vouchers.find({
         'status': {
          $in: ['OP', 'WG', 'LO', 'SO']
        },
        'details.userReachedLimitedQuantity': {
          $ne: userId
        },
      }, fields, {
        sort: {
          'order': 1
        }
      }

But this will give me all the result where userId not equal in the list. not exactly what I need. Is it better to solve using aggregate?

Example resource JSON file

[{
        "_id": "111",
        "details": {
            "limitedQuantityPerUser": "3",
            "userReachedLimitedQuantity": [{
                    "userId": "aaa",
                    "quantityBrought": "2"
                },
                {
                    "userId": "bbb",
                    "quantityBrought": "1"
                },
                {
                    "userId": "ccc",
                    "quantityBrought": "3"
                }
            ],
            "status": "OP"
        }
    },
    {
        "_id": "222",
        "details": {
            "limitedQuantityPerUser": "2",
            "userReachedLimitedQuantity": [{
                    "userId": "eee",
                    "quantityBrought": "2"
                },
                {
                    "userId": "bbb",
                    "quantityBrought": "1"
                },
                {
                    "userId": "vvv",
                    "quantityBrought": "2"
                }
            ],
            "status": "OP"
        }
    }
]

回答1:


You can use below aggregation to check whether specified userId exceeded defined limit.

db.vouchers.aggregate([
    {
        $match: {
            $expr: {
                $allElementsTrue: {
                    $map: {
                        input: "$details.userReachedLimitedQuantity",
                        as: "user",
                        in: {
                            $or: [
                                { $ne: [ "$$user.userId", userId ] },
                                { $lte: [ "$$user.quantityBrought", "$details.limitedQuantityPerUser"  ] }
                            ]
                        }
                    }
                }
            }
        }
    }
])

$allElementsTrue takes an array of boolean values as a parameter. You can use $map to transform your userReachedLimitedQuantity into such boolean values checking userId and quantityBrought fields.

So if any element is false then it means that that user's quantityBrought is greater than limitedQuantityPerUser




回答2:


No a perfect answer, but I did the filter in the result of mongod query

// Only take matched userId's quantityPurchased
const newResult = result.filter(val => {
  if (!val.details.userReachedLimitedQuantity) {
    return val;
  }
  val.details.userReachedLimitedQuantity= val.details.userReachedLimitedQuantity.filter((o) => {
    return (String(o.userId) == userId)
  });
  if (val.details.userReachedLimitedQuantity[0].quantityBrou < val.details.limitedQuantityPerUser) {
    return val;
  } else {
    return false;
  }


来源:https://stackoverflow.com/questions/53995878/mongodb-compare-two-fields-with-matched-fields-value-aggregate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!