0

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.

1 Answer 1

1

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

The implementation in your case:

db.find({someArray: {$ne: null})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.