Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-7854

Document how to check if a field itself is an array

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • manual
    • None

    Description

      Currently when the field is an array, $type operation on that field matches any inner element. { $type : 'array' } on that array field will match if the field has a nested array, and it will not return results where the field itself is an array.

      To check if a field is an array and if the array is not empty, we can use the following query:

      db.collection.find({"answers.vendor": {$elemMatch: {$exists:true}}})
      

      To return the document if the field is an array (no matter if it empty or not), we can use the following query:

      db.collection.find({$or:[{"answers.vendor": {$elemMatch: {$exists:true}}}, {"answers.vendor":[]} ]})
      

      It would be nice to add this to the document.

      Attachments

        Activity

          People

            steve.renaker@mongodb.com Steve Renaker (Inactive)
            linda.qin@mongodb.com Linda Qin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              6 years, 38 weeks, 6 days ago