yegor256
yegor256

Reputation: 105043

How to find a document with an element's value greater than size of array?

I want to find a document with x larger than the size of the array y. I'm doing this:

db.find(x: { $gt: { $size: "$y" } })

Doesn't find anything. What is wrong?

The last document should be returned:

{x: 0, y: [0, 1]}
{x: 1, y: [0, 1]}
{x: 2, y: [0, 1]}
{x: 3, y: [0, 1]} # 3 is larger than 2

Upvotes: 1

Views: 40

Answers (1)

mickl
mickl

Reputation: 49945

You need $expr to compare two fields from the same document in your query:

db.col.find({ $expr: { $gt: [ "$x", { $size: "$y" } ] } })

Upvotes: 1

Related Questions