Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo {$ne : null} not working as expected

Tags:

null

mongodb

When I issue the following query:

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

I expect to receive 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 receive users whose pic.status IS null. How can I fix this?

like image 841
Jens Ljungblad Avatar asked Apr 03 '12 14:04

Jens Ljungblad


People also ask

How do you write NOT NULL condition in MongoDB?

To query for is not null value, we can use the $ne operator as well as the $eq operator and specify the desired value that we want to query for. This guide aims to provide readers with different ways and examples for the same to query for is not null values in MongoDB.

How do I display null values in MongoDB?

MongoDB fetch documents containing 'null' If we want to fetch documents from the collection "testtable" which contains the value of "interest" is null, the following mongodb command can be used : >db. testtable. find( { "interest" : null } ).

IS NULL condition in MongoDB?

The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. The query returns both documents in the collection.

How do I search in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


2 Answers

I know it's an old question, and might not be relevant anymore, but as there was no accepted answer, I thought I'll comment for anyone looking for 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 on Version 2.6.1:

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

Probably was fixed somewhere in between these two versions.

like image 169
uiron Avatar answered Sep 24 '22 07:09

uiron


I cannot seem to reproduce this. What version of Mongo are you using? (I am using 2.1.1-pre) Here are the steps that I took. The following is from the JS shell:

> 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"
        }
    }
]

Only the document containing "pic.status":"Something" is returned.

My only thought is, are you absolutely certain that the value of (null) in the query is the same as what is saved in the documents? Were the documents saved using the JS shell, or were they saved using a driver in a different language? Theoretically, null, should be null, should be null, in any language, but I know that different languages represent "no value" differently. In python, for example, the value for "no value" is (None). (null) is not recognised in python.

In a python shell, I attempted to save a document with (null) as a value, but received an error:

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

I then inserted (with the Python shell) a status of 'null' ('null' being a string)

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

Not surprisingly, when I reran the query in the JS shell, this document was returned, because '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
        }
    }
]

Is this similar to what you are experiencing, or can you reproduce this 100% in the JS shell? Hopefully, we will be able to get to the root of this issue!

like image 32
Marc Avatar answered Sep 23 '22 07:09

Marc