lemontree
lemontree

Reputation: 99

Setting up a query to look at a specific index of an array of objects

I'm Having trouble with setting up a query - it is an array of objects, and I'm looking at a specific index. So my Schema has a field that is an array of objects:

{userID: ObjectID,
someArray: [{foo: "bar"},{bar: "foo"}]}

I'd like to return either all records where the first element in SomeArray has a non-empty object or where someArray does not contain null.

Ive tried using:

 db.find({someArray: {$exists: true, $not: {$size: 0}} })

however often someArray has a null object in it so that doesn't work. If all fails I could potentially cache the entire collection and filter with raw javascript though that might not be the ideal way of doing it.

Upvotes: 0

Views: 30

Answers (1)

Arm1stice
Arm1stice

Reputation: 101

To solve your problem, I suggest using the $ne (not-equal) helper

The implementation in your case:

db.find({someArray: {$ne: null})

Upvotes: 1

Related Questions