1

My objective is to query the array fields who have size less than 5.

I tried the below solution

MongoDatabase database = mongoClient.getDatabase("Friends");
        MongoCollection<Document> collection = database.getCollection("Friend");

        BasicDBObject filter = new BasicDBObject("MainArray", new BasicDBObject("$lt", new BasicDBObject("$size", 4)));
        collection.find(filter).forEach((Consumer<Document>) doc -> {
//perform operations
}

I want to fetch the fields whose size is less than 4.

If I directly put the size 4 -- new BasicDBObject("$size",4) It did works.

But I want to fetch the fields with array size less than 4 In that case it didn't work.

Please provide your valuable suggestions.

Rishabh
  • 125
  • 7

1 Answers1

1

You are not using annotations.

{$expr:{$lt:[{$size:"$MainArray"}, 5]}}

You need to use $expr to use operators in simple find.

Equivalent in BasicDBObject is

new BasicDBObject("$expr", 
       new BasicDBObject("$lt", 
            Arrays<DBObject>.asList(new BasicDBObject("$size", "$MainArray"), 5)
       )
)
Gibbs
  • 21,904
  • 13
  • 74
  • 138
  • Hey thanks this looks promising I do remember this works in mongodb ...but it's giving error at Arrays. Have I missed something? Do we need to import something? It's giving unexpected token, Expression expected, Cannot resolve DBObject. – Rishabh May 23 '22 at 16:31
  • You can use BasicDBObject in place of DBObject – Gibbs May 23 '22 at 16:32
  • Now it's showing Expression expected, ')' expected, ;expected at Arrays – Rishabh May 23 '22 at 16:34
  • Please add one extra `)` at the end. Updating the answer – Gibbs May 23 '22 at 16:35
  • No problem....although I used the correct braces -- BasicDBObject filter = new BasicDBObject("$expr", new BasicDBObject("$lt", Arrays.asList(new BasicDBObject("$size","$MainArray"),5))); But it's showing the same error – Rishabh May 23 '22 at 16:39
  • Hey @Gibbs Strange to see neither did the mongodb query worked. It says the argument to $size must be array, but was of type: missing Although I can clearly see in MongoDb Compass it's an array and even it's mentioned. Then I was reading this question -- https://stackoverflow.com/questions/24201120/mongodb-the-argument-to-size-must-be-an-array-but-was-of-type-eoo-missing Can you please suggest something – Rishabh May 23 '22 at 18:25
  • Same brackets error still? Can you post your updated query? – Gibbs May 24 '22 at 03:24
  • Sure...Sorry it's my fault. I think so I must update the question. Because I the array whose size I want to fetch is a little deeper inside the document. Please check the updated question again. I'm adding a document in json format. MongoQuery was -- db.collectionName.find({"$expr":{$gte:[{$size:"$Main_Array.address"},2]}}) – Rishabh May 24 '22 at 06:19
  • Maybe due to fetching the size of the 'address' array we are getting the errors. Do you have any idea how should we modify the query? – Rishabh May 24 '22 at 06:54
  • You have modified your entire question. Since you have nested arrays, you need to work on your mongo queries first. I would suggest you to post another question. – Gibbs May 24 '22 at 08:48
  • 1
    Okay ..sure man understood Thanks for helping And sorry for the inconvenience caused. I really appreciate you for helping me. I will try to edit the question again and will mark your answer as correct. Thanks – Rishabh May 24 '22 at 13:47