Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select where not null or empty in mongoid

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.

like image 296
methodofaction Avatar asked Apr 26 '12 16:04

methodofaction


People also ask

How do I specify NOT NULL in MongoDB?

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.

How do I filter non null 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.

IS NULL condition in MongoDB?

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 } ).

Where is null field in MongoDB?

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.


1 Answers

Try

Model.where(:url.ne => "", :url.exists => true).count 

see Mongoid Symbol Operators

like image 172
Kyle Avatar answered Oct 20 '22 20:10

Kyle