Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

343
Views
Mongo: Query for documents based on nested array length

I have a document structure that looks like this:

{
key1:value1,
key2:value2,
key3: [
    index0: objectID(...),
    index1: [
          index0: objectID(...)
         ]
      ]
}

I know you can access arrays using Mongo's dot notation, but I am trying to build a query that only selects all records where key3.index1 length is greater than zero.

All documents in this collection will have key3 and key3 will always have an array at index1. In some cases this array will not be empty.

So far I've tried something like this: db.collection.find({$where: "this.key3.1.length > 0"}) but this doesn't work.

How can I query for documents using the length of a nested array?

EDIT: Sample Data

{
    "_id" : ObjectId("57c98fe77fe6f8009401b5d7"),
    "arrayOfData" : [ 
        ObjectId("57c98fe87fe6f8009401b5d8"), 
        [ 
            ObjectId("57c98fe87fe6f8009401b5d9")
        ]
    ]
}

The above is the relevant snippet from the document structure. All documents will have this, in some cases the nested array will be empty, in some cases it will not.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!