Search code examples
arangodb

How do I count of how many documents the field x is empty inside a certian collection?


I have problem. I have a collection orders. I want to check how many documents with the field phone are empty.

So how could I count of how many documents the field phone is empty inside the collection orders?

This my collection orders:

[
{'_id': 'orders/213123',
 'contactEditor': {'name': 'Max Power',
  'phone': '1234567',
  'email': '[email protected]'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': '[email protected]'},
 'isCompleteDelivery': False,
 'metaData': {'dataOriginSystem': 'Goods',
  'dataOriginWasCreatedTime': '10:12:12',},
 'orderDate': '2021-02-22',
 'orderDateBuyer': '2021-02-22',
},
{'_id': 'orders/12323',
 'contactEditor': {'name': 'Max Power2',
  'phone': '1234567',
  'email': '[email protected]'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': '[email protected]'},
 'isCompleteDelivery': False,
 'metaData': {'dataOriginSystem': 'Goods',
  'dataOriginWasCreatedTime': '10:12:12',},
 'orderDate': '2021-02-22',
 'orderDateBuyer': '2021-02-22',
 },
]

Solution

  • If you're trying to FILTER for a value that is null...

    FOR o IN orders
        FILTER o.contactSoldToParty.phone == null
        RETURN o
    

    But if you just want a simple count, then I would use COLLECT (see the docs)...

    FOR o IN orders
        COLLECT hasPhone = (o.contactSoldToParty.phone != null) WITH COUNT INTO total
        RETURN { hasPhone, total }
    

    There are two caveats, both related to how your document is structured:

    1. you may have to first check if the contactSoldToParty attribute exists (or use nullish coalescing)
    2. be sure that the phone attribute is indeed empty/missing/null - null is not the same as an empty string ('')