Mongo and Mongoid checking if an array is empty
First attempt in Mongoid:
{ foo: { $not: { $size: 0 } } }
This gets translated to mongoid with the following:
User.where(:foo.size => 0)
:) ENJOY!!
First attempt in Mongoid:
{ foo: { $not: { $size: 0 } } }
This gets translated to mongoid with the following:
User.where(:foo.size => 0)
:) ENJOY!!
First step Mongo shell query:
db.yourcollection.find({ 'deleted_at' : { '$exists' : true }})
This query in mongo shell will actually pull all the records for which the field deleted_at is not null.
@posts = Posts.where(:created_at.lte => (Date.today - 2), :author => “BlogMaster”, :likings.exists => false, :comments.exists => false)
This instead will pull all the records older than 2 days ago, with author BlogMaster and for which we don’t have any likings nor comments.
Enjoy!
This is a note for myself for a project I am actually working on that I thought could be useful to share :)
More info here.
A document can recursively embed itself using recursively_embeds_many, which provides accessors for the parent and children.
class Tag include Mongoid::Document recursively_embeds_many field :name end programming = Tag.new(:name => 'programming') ruby = programming.child_tags.build(:name => 'ruby') python = programming.child_tags.build(:name => 'python') programming.child_tags # [ruby, python] ruby.parent_tag # [programming] python.parent_tag # [programming]