简体   繁体   中英

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': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': 'maxnot@power.com'},
 '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': 'max@power.com'},
 'contactSoldToParty': {'name': 'Max Not',
  'phone': '123456789',
  'email': 'maxnot@power.com'},
 'isCompleteDelivery': False,
 'metaData': {'dataOriginSystem': 'Goods',
  'dataOriginWasCreatedTime': '10:12:12',},
 'orderDate': '2021-02-22',
 'orderDateBuyer': '2021-02-22',
 },
]

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 ( '' )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM