How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
For example, I have collection with documents, where documents can have field "url" (bu开发者_StackOverflow社区t most of them doesn't). How can I find all documents, which have field "url" (regardless of value of this field)?
To find if a key/field exists in your document use the $exists operator.
Via the MongoDB shell ...
> db.things.find( { url : { $exists : true } } );
db.things.find( { url : { $exists : true } }, {url:1, _id:1});
if you changed you mind and you want to display just the url without the _id you have to change the value of the _id key to 0.
精彩评论