0 votes
1 view
in SQL by (47.7k points)

All of my records have a field called "pictures". This field is an array of strings.

I now want the newest 10 records where this array IS NOT empty.

I've googled around, but strangely enough, I haven't found much on this. I've read into the $where option, but I was wondering how slow that is to native functions, and if there is a better solution.

And even then, that does not work:

ME.find({$where: 'this.pictures.length > 0'}).sort('-created').limit(10).execFind()

Returns nothing. Leaving this.pictures without the length bit does work, but then it also returns empty records, of course.

1 Answer

0 votes
by (107k points)

To find MongoDB records where array field is not empty you have documents that don't have the key, you can use below:-

ME.find({ pictures: { $exists: true, $not: {$size: 0} } })

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !