Working of `$exists: true` vs `$ne: null` with array fields

We have been using $ne: null with our array fields(object type) for mongo queries instead of $exists: true . Problem is when using $ne, it doesn’t give the right result. The code is in Node where it is not working. Plus when we tried with compass, it worked with correctly in mine, but didn’t in my collugue’s. Not sure what is happening.

Example query:
Document
``` [{name: ‘Event1’, ‘arrayfield’: [{id: ‘id1’}]}]`

Query
{arrayField.eID: {$exists: true}}` // works {arrayField.eID: {$ne: null}}` // doesn’t work

Example query:
Document

[{name: ‘Event1’, ‘arrayfield’: [{id: ‘id1’}]}]
`

Query

{arrayField.eID: {$exists: true}} // works


`
{arrayField.eID: {$ne: null}}  // doesn’t work
`

Your code is wrong.

You are querying the field arrayField.eID and your sample document is arrayField.id. So I really think that

does not work.

So the fact that it works in your case and not with your colleague is most like a typing error.

@Abhishek_Taparia, 7 days since I replied to your OP. I would appreciate a followup.

Thanks