开发者

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

开发者 https://www.devze.com 2023-02-01 01:20 出处:网络
For example, I have collection with documents, where documents can have field \"url\" (bu开发者_StackOverflow社区t most of them doesn\'t).

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号