I modified a model so it includes a new field, such as...
field :url, :type => String
I use activeadmin, so when I create a new entry @model.url
is empty, and in entries created before changing the schema it's nil. How do I select both? I have tried:
# Returns nils and strings Model.where(:url.ne => "").count # Returns strings and "" Model.where(:url.ne => nil).count # Returns strings, nils and "" Model.where(:url.ne => ["", nil]).count
Or, if there's a best practice for this kind of scenario please let me know.
To query for is not null value, we can use the $ne operator as well as the $eq operator and specify the desired value that we want to query for. This guide aims to provide readers with different ways and examples for the same to query for is not null values in MongoDB.
This MongoDB article will explain how you can query non-null values in MongoDB. To query for the is not null value, you can use the $ne operator and the $eq operator, then specify the desired value that you want to query for.
MongoDB fetch documents containing 'null' If we want to fetch documents from the collection "testtable" which contains the value of "interest" is null, the following mongodb command can be used : >db. testtable. find( { "interest" : null } ).
The { item : null } query matches documents that either contain the item field whose value is null or that do not contain the item field. The query returns both documents in the collection.
Try
Model.where(:url.ne => "", :url.exists => true).count
see Mongoid Symbol Operators
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With