Array | Query For Documents Where Array Size Is Greater Than 1

In This tutorial, we will see Query For Documents Where Array Size Is Greater Than 1 which we can do in two ways first with the use of not equal to Operator and other by MongoDB quarry for array grater there 0, 1, and 2.


Let’s see both the method one by one and understand them before they let have a json where the order of the array is defined.

  "_id": "617 f1b826cb cd5cf388 e44f7",
  "Ssn": { 
    "$binary": "AQN8wobD t3M8Ss OPwrjDsHNVw 5Ymwo3Dl QJ1AQpYwrhyw 6oIwp/CgAzCp CHDqcKBwrDCjTo +w4/CsMO 0wozDmmfCuxE7w4ty  wofDoR/Dh MOgw6HDhsKZwoR LwovCu8K DXQTCncKbL 11nwpRQBMKeT cKLFlXC vwYxwrxUwoo=",
    "$type": "0"
  "City": "NY",
  "Country": "USA",
  "States": "NY",
  "Zip": "12345",
  "order":  [ "name1",  "name2", "name3" ],
  "Name": "John"

Ways Two Quary For Array Size Greater than 1

there are two ways to do so and here we will follow both one by one.

By Using Not Equal To Operator

{order: {$type: 'array', $ne: []}}

The above command will confirm whether the array has at least one record stored and it will run without any error. And it will result in an array of sizes 1 and 2. as in JSON we declared an array of 3 entries earlier.

MongoDB Query For Array Of Documents Greater than 0, 1, and 2

{'order.0': {$exists: true}}
{'order.1': {$exists: true}}
{'order.2': {$exists: true}}


The above commands will check for zero, One, and two recodes in the document array in MongoDB.

Learn More about Array | Query For Documents Where Array Size Is Greater Than 1 at MongoDB commands and learn more about Mongo DB Different Topics.

And also visit MongoDB Tutorial and Problems for learning more about java and the solutions of core java and tutorials for different problems faced during learning and solving the problems of java.

And for learning more about python at Python’s list: What is the difference Between methods Append and Extend? and learn more about all the different types of programming languages and keep growing by learning new things and solving problems.


Leave a Comment

%d bloggers like this: