0

MongoDB query to get last/all objects from array

{
    WholeData:[
        {
            EnteredAmount:100,
            OverPayment:0,
            Type:"payment",
        },
        {
            EnteredAmount:200,
            OverPayment:0,
            Type:"payment",
        },
    ]
}

{
    WholeData:[
        {
            EnteredAmount:600,
            OverPayment:0,
            Type:"refund",
        },
        {
            EnteredAmount:400,
            OverPayment:0,
            Type:"refund",
        },
    ]
}

This is how sample documents looks like
I want to write a query which results something like if type is refund then push/keep all array objects else push/keep only the last index object. sample output should be

{
    WholeData:[

        {
            EnteredAmount:200,
            OverPayment:0,
            Type:"payment",
        },
    ]
}



{
    WholeData:[
        {
            EnteredAmount:600,
            OverPayment:0,
            Type:"refund",
        },
        {
            EnteredAmount:400,
            OverPayment:0,
            Type:"refund",
        },
    ]
}
2
  • it's something you can't translate to a single query. Commented Sep 9, 2021 at 12:14
  • its a long query already. that alright if it can be done in multiple pipelines Commented Sep 9, 2021 at 12:29

1 Answer 1

1

first we create a another array to store last element of WholeData if Type:"payment" then create project and check if WholeData was empty means type is refund else payment and create our result use this aggregation

db.collection.aggregate([
[
   {
    '$addFields': {
      'WholeDataP': {
        '$filter': {
          'input': '$WholeData', 
          'as': 'z', 
          'cond': {
            '$and': [
              {
                '$eq': [
                  {
                    '$indexOfArray': [
                      '$WholeData', '$$z'
                    ]
                  }, {
                    '$sum': [
                      {
                        '$size': '$WholeData'
                      }, -1
                    ]
                  }
                ]
              }, {
                '$ne': [
                  '$$z.Type', 'refund'
                ]
              }
            ]
          }
        }
      }
    }
  }, {
    '$project': {
      'WholeData': {
        '$cond': [
          {
            '$eq': [
              {
                '$size': '$WholeDataP'
              }, 0
            ]
          }, '$WholeData', '$WholeDataP'
        ]
      }
    }
  }
]
])
Sign up to request clarification or add additional context in comments.

2 Comments

you understand it a bit different if type is refund then all array element other wise last element and type can be payment, invoice or some other strings only for refund i need all elements and for all other i need last element
yes i did exactly the same. changed $eq to $ne and refund and your change is also same. If you can explain little the and portion of the query whats exactly happening in that whole aggregation. Thanks.

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.