MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Help with filtering in query

Lets say I have created some records

db.getCollection('my-collection').insertMany([{x: 0, y: 0, z: 0},
                                                                       {x: 1, y: 0, z: 0},
                                                                       {x: 0, y: 1, z: 0},
                                                                       {x: 0, y: 0, z: 1}])

and I want to fetch all records that doesnt have x,y and z equal to zero
so the first record would be filtered out from the results because all of its fields are equal to zero
and I would get as a result

[{x: 1, y: 0, z: 0}, {x: 0, y: 1, z: 0}, {x: 0, y: 0, z: 1}]

You are trying this:

if the value of any of the fields (x, y, z) is not equal to zero
then print the document 
else ignore

So, the filter is: { $or: [ { x: { $ne: 0 } }, { y: { $ne: 0 } }, { z: { $ne: 0 } } ] }

You can also try the following with the same result:

if the value of any of the fields (x, y, z) is greater than zero
then print the document 
else ignore

[Also, try with less than instead of greater than condition].