1

I have data stored in MongoDB as following:

{
    "payouts": {
      "entries": [
        {
          "custom_payout_overwrite": false, 
          "entry_name": "", 
          "network_custom_payout_revenue_setting_id": 0, 
          "network_offer_payout_revenue_id": 1, 
          "payout_amount": 0.44, 
          "payout_percentage": 0, 
          "payout_type": "cpa"
        }
      ], 
      "total": 1
    }
}

I want to get only data which has payout_amount greater than 0.

I have already tried

filter_by = {"payouts.entries.payout_amount": {"$gte": 0}}
data = db[collection_name].find(filter_by)

But it's not working I gues. It's because entries is a list and I am not sure how to query further inside a list. Thanks.

3
  • Would you like to get documents or specific entries of entries array? Commented Feb 1, 2018 at 8:56
  • Documents please Commented Feb 1, 2018 at 8:59
  • Tried your query and it works. Could it be that you are querying the wrong collection? Commented Feb 1, 2018 at 9:06

1 Answer 1

1

You can use $elemMatch:

filter_by = {'payouts.entries': {'$elemMatch': {'payout_amount': {'$gte': 0}}}}
data = db[collection_name].find(filter_by)

Docs about $elemMatch usage is here.

Also note: to get only data which has payout_amount greater than 0 you should use $gt (not $gte):

filter_by = {'payouts.entries': {'$elemMatch': {'payout_amount': {'$gt': 0}}}}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks tried this but its still giving entries which has amount 0 :(
@Kishan have you used $gt or $gte ?
Sorry, my bad. some data has multiple "entries" in the array. so it returns that if any one of the amounts is greater than 0. 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.