I've just updated one of my models with a boolean field. I've set a default value for the field to true. How can I query for this field in such a way that I get all documents with this field set to true or doesn't have this field (default value).
相关问题
- MongoDB can not create unique sparse index (duplic
- Spring Data MongoDB - lazy access to some fields
- Golang mongodb aggregation
- How to convert from Timestamp to Mongo ObjectID
- MongoDB Indexing: Multiple single-field vs single
相关文章
- mongodb有没有什么办法禁止读取数据的时候进行缓存
- mongodb-aggregate聚合查询分组后如何获得多字段
- mongodb error: how do I make sure that your journa
- How to track MongoDB requests from a console appli
- MongoError: cannot infer query fields to set, path
- Pymongo $in Query Not Working
- django.core.exceptions.ImproperlyConfigured: '
- How to represent an array with mixed types
To find documents that don't have a particular key, you want to use
$exists
:So the existence check would look like:
Note that the first
:field.exists
form becomes the second form before it gets sent to MongoDB; I mention this because you won't be able to use:field
elsewhere in the query without using$and
or$or
to combine the clauses: the:field.exists
expansion can lead to keys in the query Hash overwriting each other. You won't have this problem here but a reminder can't hurt.Looking for
true
is easy:You want either one so combine them with
$or
:If
:field
might be there but have anull
value then you could use{ :field => nil }
to match documents where:field
isnull
or not there at all:There's also
{ :field => { :$type => 10 } }
if you're looking for things that are there and explicitlynull
. Now might be a good time for a quick review of the MongoDB FAQ: