Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use MongoDB to find all documents which have a field, regardless of the value of that field?

Tags:

mongodb

For example, I have collection with documents, where documents can have field "url" (but most of them doesn't). How can I find all documents, which have field "url" (regardless of value of this field)?

like image 441
Mikhail Avatar asked Dec 26 '10 20:12

Mikhail


1 Answers

To find if a key/field exists in your document use the $exists operator.

Via the MongoDB shell ...

> db.things.find( { url : { $exists : true } } );
like image 61
Justin Jenkins Avatar answered Oct 24 '22 11:10

Justin Jenkins