An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:
Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:
And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.
This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use count() in this way (for my example it will return 6 not 4), so please only use when appropriate.
You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.
N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
db.collection_name.find({"filed_name":{$ne:null}});
Extra validation:
Okay, so we are not finished yet we need an extra condition.
An alternative that has not been mentioned, but that may be a more efficient option for some (won't work with NULL entries) is to use a sparse index (entries in the index only exist when there is something in the field). Here is a sample data set:
Now, create the sparse index on imageUrl field:
Now, there is always a chance (and in particular with a small data set like my sample) that rather than using an index, MongoDB will use a table scan, even for a potential covered index query. As it turns out that gives me an easy way to illustrate the difference here:
OK, so the extra documents with no
imageUrl
are being returned, just empty, not what we wanted. Just to confirm why, do an explain:So, yes, a
BasicCursor
equals a table scan, it did not use the index. Let's force the query to use our sparse index with ahint()
:And there is the result we were looking for - only documents with the field populated are returned. This also only uses the index (i.e. it is a covered index query), so only the index needs to be in memory to return the results.
This is a specialized use case and can't be used generally (see other answers for those options). In particular it should be noted that as things stand you cannot use
count()
in this way (for my example it will return 6 not 4), so please only use when appropriate.db.collection_name.find({"filed_name":{$exists:true}});
fetch documents that contain this filed_name even it is null.
My proposition:
You can check on the required attribute's type, it will return all the documents that its field_name queried contains a value because you are checking on the filed's type else if it is null the type condition doesn't match so nothing will be returned.
N.b:if the field_name has an empty string which means "", it will be returned.It is the same behavior for
db.collection_name.find({"filed_name":{$ne:null}});
Extra validation:
Okay, so we are not finished yet we need an extra condition.
db.collection_name. find({ "field_name":{$type:2},$where:"this.field_name.length >0"})
OR
db.collection_name. find({ "field_name":{$ne:null},$where:"this.field_name.length >0"})
Reference for all the types: https://docs.mongodb.com/manual/reference/operator/query/type/#op._S_type