MongoDB Implicit And I'm trying to understand mongodb queries, but one of my queries is returning a record that it shouldn't.

I am trying to call

db.user.find({'referral.money':{'$ne':0, '$ne':null}})


to get all users who have some cents in their referral data. Some users have referral data which doesn't include "money" and others don't have any referral data at all.

The problem is that after executing that command, the following record is returned:

{
    "_id" : ObjectId("58362414af5d810d1c9ec7f0"),
    "user_id" : 22,
    "referral" : {
        "_cls" : "ReferralProgramA",
        "end_dtm" : ISODate("2016-12-23T23:19:48.014Z"),
        "money" : 0
    }
}


If I'm correct it shouldn't be returned since "money" is obviously 0.

My collections and records are created with python's mongoengine hence the "_cls" fields for inheritance.

I'm making the find({}) call with a raw statement so mongoengine isn't messing it up( I think ).
The problem is that your query object uses the same key ($ne) twice, so the second value is overwriting the first. Most languages do not support duplicate keys in a standard object / hash / dictionary representation.

The outcome is more evident if you evaluate your query object in the mongo shell:

&gt; query = {'referral.money':{'$ne':0, '$ne':null}}
{
  "referral.money": {
    "$ne": null
  }
}


However, you could use a different operator with equivalent outcome in an implicit $and.

For your example query, $exists: true is equivalent to $ne: null so the following would work:

 db.user.find({'referral.money':{'$ne':0, $exists: true}})


Alternatively you could rewrite the query to use an explicit $and:

 db.user.find({$and: [{'referral.money': {'$ne':0}}, {'referral.money':{'$ne':null}} ]})


Note: if you are storing currency values as floating points you should be wary of rounding with floating point numbers. The MongoDB manual has examples of how to Model Monetary Data.