0

How can I use the $min function to get the min value within nested arrays (and add it to the document)?

[
  {
    "_id": "a357e77f-a76a-4bc2-8765-923280663e97",
    "customers": [
      {
        "_id": "97170117-4660-4c6f-b8da-2b34d4d0c9ce",
        "orders": [
          {
            "amount": 0.5
          },
          {
            "amount": 6.400001525878906
          }
        ]
      },
      {
        "_id": "7b9ccf5b-3acb-4ed1-8df4-e3b5afc49cba",
        "orders": [
          {
            "amount": 27.29999542236328
          },
          {
            "amount": 0.29999542236328125
          }
        ]
      }
    ]
  },
  {
    "_id": "58433224-8162-4f0a-8168-bc11b4306b0a",
    "customers": [
      {
        "_id": "8a6055d0-9b94-40be-8f96-8fd9088d24aa",
        "orders": [
          {
            "amount": 19.700000762939453
          }
        ]
      },
      {
        "_id": "a50a57b8-61e7-4727-a15a-4a4137b2f81a",
        "orders": [
          {
            "amount": 43.80000305175781
          }
        ]
      }
    ]
  }
]

How can I get the min amount value within the $customers.orders.amount path?

I've tried but it returns 0.

db.collection.aggregate([
  {
    $addFields: {
      "amount": {
        $sum: "$customers.orders.amount"
      }
    }
  }
])

3
  • Where is amount? Are you referring to total? Commented Dec 10, 2020 at 7:19
  • Yes. Sorry, updated the question. Commented Dec 10, 2020 at 8:31
  • Min value across all the customers? Commented Dec 10, 2020 at 8:40

2 Answers 2

1

You can do as below for each customer

playground

db.collection.aggregate([
  {//Destruct
    "$unwind": "$customers"
  },
  {//Destruct
    "$unwind": "$customers.orders"
  },
  {//Group by customer id,
    $group: {
      "_id": "$customers._id",
      min: {
        $push: {
          "$min": "$customers.orders.amount"
        }
      }
    }
  }
])

You can use group by null if you want to find min across all the customers.

Sign up to request clarification or add additional context in comments.

1 Comment

That returned a min for each customer rather than across each root document (e.g. a site). Sorry if I wasn't clear. Also wanted to add the min to the existing documents. However I have found a way using reduce.
0

Found a solution using reduce to create a flat array of the nested arrays and then use $min on that. MongoPlayground

db.collection.aggregate([
    {
        "$addFields": {
            "min": {
                $min: {
                    "$reduce": {
                        "input": "$customers",
                        "initialValue": [],
                        "in": {
                        "$concatArrays": [
                                "$$value",
                                "$$this.orders.amount"
                            ]
                        }
                    }
                }
            }
        }
    }
])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.