Need help with MongoDB query to return those documents that have a length of array element greater than 5 or 9

I have a collection and need to query on all documents where the serviceArea.regions.zipCodes have a length greater than 5 or 9, I’ve tried different approaches using $where and also $aggregate function but didn’t have any luck.


serviceArea is an object and zipCodes is an array with elements:

eg. serviceArea{regions:[(countryCode:“US”,zipCodes[“50010”,“50011”,“50012”])]}

so it should be serviceArea.regions.zipCodes that I need to refer to and give me back results for zipCodes that are greater than 5 or 9

Any help would be much appreciated.

The following is not clear to me.

Please provide sample documents and result documents. Make sure you include documents that matches and some that do not matches what you want.

This post was flagged by the community and is temporarily hidden.

I’ve uploaded an image of the document structure, so basically I want to get back all documents that have serviceArea.regions.zipCodes elements greater then 5 or 9 digits. In this example you will see elements displaying zipCodes with 5 digits.

Please provide real documents that we can copy directly into our installation. Retyping documents to test our idea to solve your issue is time consuming.

You repeated the same sentence as the original post. It is not clearer. Do you want that in the same query or in 2 different queries?

You might be interested in

1 Like

So in the same query, for example if I want to get back documents with zipCodes that have 6, 7, 8, 10, 11 digits. Does that make sense?

Document 1
Zipcodes - 510192
Document 2
ZipCodes - 5101932
Document 3
ZipCodes - 5101934
Document 4
ZipCodes - 51019345
Document 5
ZipCodes - 5101934567

Here is an example, does this work?

	"zone" : "DC",
	"active" : true,
	"akas" : [
			"name" : "Testing"
			"name" : "Testing"
	"displayName" : "Testing",
	"serviceArea" : {
		"regions" : [
				"zipCodes" : [

Have you tried to incorporate the information from the 2 links supplied?

What have you tried? What issues did you get?