Mongo {$ ne: null} does not work as expected

When I issue the following request:

db.users.find({"pic.status" : {$ne : null} }, {"pic" : 1}).toArray()

I expect to get all users whose pic.status is not null. However, the actual result looks something like this:

{                                                                                                                                      
    "_id" : ObjectId("4f1e1ab9cdf9dbaa160000bf"),
    "pic" : {
        "id" : "4f1e1ab9cdf9dbaa160000be",                                                                                                                                                                                                                                                                                                                      
        "status" : null
    }
},
{
    "_id" : ObjectId("4f1e28480eaf38193d00006f"),
    "pic" : {
        "id" : "4f1e28480eaf38193d00006e",                                                                                                                                                                                                                                                                                                                      
        "status" : null
    }
}

That is, I get users whose pic.status IS is null. How can i fix this?

+5
source share
3 answers

I know this is an old question, and maybe this is no longer relevant, but since no answer was accepted, I thought I would comment on anyone who is looking for an answer.

I was able to reproduce the problem on MongoDB version 2.4.9.

> db.sourceListings.findOne({displayed:{$ne:null}});
{
   <.. other stuff went here..>
   "displayed" : null
}

The problem disappears in version 2.6.1:

> db.sourceListings.findOne();
{
   <.. other stuff ..>
   "displayed" : null
}
> db.sourceListings.findOne({displayed:{$ne:null}});
null

It was probably fixed somewhere between the two versions.

+4
source

. ? ( 2.1.1-pre) , . JS :

> db.users.save({                                                                                                                                      
    "_id" : 1,
    "pic" : {
        "id" : "4f1e1ab9cdf9dbaa160000be",                                                                                                                                                                                                                                                                                                                      
        "status" : null
    }
});
> db.users.save({
    "_id" : 2,
    "pic" : {
        "id" : "4f1e28480eaf38193d00006e",                                                                                                                                                                                                                                                                                                                      
        "status" : null
    }
});
> db.users.save({
    "_id" : 3,
    "pic" : {
        "id" : "4f1e28480eaf38193d00006e",                                                                                                                                                                                                                                                                                                                      
        "status" : "Something"
    }
});
> db.users.find({"pic.status":{$ne:null}}, {pic:1}).toArray()
[
    {
        "_id" : 3,
        "pic" : {
            "id" : "4f1e28480eaf38193d00006e",
            "status" : "Something"
        }
    }
]

, "pic.status": "-".

: , (null) , ? JS ? , null, , , , , " " -. , python "no value" (None). (null) python.

python (null) , :

In [13]: coll.save({"_id":5, "pic":{"id":5, "status":null}})
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/Users/mbastien/mongodb-osx-x86_64-2.0.1/bin/<ipython-input-13-1ad232456c88> in <module>()
----> 1 coll.save({"_id":5, "pic":{"id":5, "status":null}})

NameError: name 'null' is not defined

( Python) "null" ( "null" - )

In [15]: coll.save({"_id":5, "pic":{"id":5, "status":'null'}})
Out[15]: 5

, JS, , 'null'!= null

> db.users.find({"pic.status":{$ne:null}}, {pic:1}).toArray()
[
    {
        "_id" : 3,
        "pic" : {
            "id" : "4f1e28480eaf38193d00006e",
            "status" : "Something"
        }
    },
    {
        "_id" : 5,
        "pic" : {
            "status" : "null",
            "id" : 5
        }
    }
]

, , 100% JS? , !

+2

Use $ nin with one element in an array. He will behave as you expect. KI with many people who use mongo.

0
source

All Articles