I'm having a difficult time doing something such as:
Something.where(:field => nil)
or
Something.where(:field => { '$eq' => nil })
What's the right way to handle this in Mongoid?
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 } ).
In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).
Use BsonNull. Value with the MongoDB C# driver to query for null or missing fields in MongoDB.
Indeed, it's not possible to store null values in a MongoDB document using a DataFrame. The Python None values are considered as missing attributes accordingly to this NoSQL specific allowance. However, if your column is numerical, you can force writing a null value by setting it to NaN.
That's the right way to do it. To find cars whose engine is nil
, for example, use:
# Cars that have a _nil_ engine.
Car.where(:engine => nil)
If you're trying to look for the absence of a field (rather than one that's set to nil
), use the $exists
predicate:
# Cars that lack an engine entirely.
Car.where(:engine.exists => false)
Note that setting a field foo
to be nil
and lacking a field named foo
are two different things.
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