$exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.
$ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
So in your provided case following query going to return all the documents with imageurl field exists and having not null value:
This will return all documents with a key called "IMAGE URL", but they may still have a null value.
This will return all documents with both a key called "IMAGE URL" and a non-null value.
Also, according to the docs, $exists currently can't use an index, but $ne can.
Edit: Adding some examples due to interest in this answer
Given these inserts:
This will return all three documents:
This will return the first and second documents only:
This will return the first document only:
This will return the second and third documents only:
the Query Will be
it will return all documents having "IMAGE URL" as a key ...........
This query worked for us (query executed from MongoDB compass):
In pymongo you can use:
Because pymongo represents mongo "null" as python "None".
One liner is the best :
Here,
mycollection : place your desired collection name
fieldname : place your desired field name
Explaination :
$exists : When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.
$ne selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
So in your provided case following query going to return all the documents with imageurl field exists and having not null value: