Can mongoid be used for this kind of query

47 views
Skip to first unread message

TheR

unread,
Mar 5, 2014, 1:46:33 AM3/5/14
to mon...@googlegroups.com
I have a news collection with documents, which may (or may not) have limited time of activity.

I have 3 fields which indicate if news is valid.  active, valid_from, valid_to. Normaly I would do this kind of query:

where active = true and (valid_from = null or valid_from < Time.now) and (valid_to= null or valid_to > Time.now). OK. Time.now is ruby only, but I hope you understand.

Can this be done with mongoid and how.


Thank you
TheR


TheR

unread,
Mar 10, 2014, 2:50:56 AM3/10/14
to mon...@googlegroups.com
To reply my own question. It can.

.where(active: true)
.and({"$or" => [{:valid_from => nil}, {:valid_from.lt => Time.now}]}, {"$or" => [{:valid_to => nil}, {:valid_to.gt => Time.now}]}) 


by
TheR

Reply all
Reply to author
Forward
0 new messages