开发者

MongoDB date range queries (with optionnal dates)

开发者 https://www.devze.com 2023-03-17 12:07 出处:网络
I have objects with optionnal startDate and endDate properties I\'d like to query my objects like : startDate equals null or greater

I have objects with optionnal startDate and endDate properties

I'd like to query my objects like :

startDate equals null or greater than today AND endDate equals null or lesser than today.

If I had only one single date, I would of course use the "or" operator

{$or : [{startDate : null}, {start开发者_如何学运维Date : {$gte : new Date()}}]}

I could then make a complicated request like

{$or : [{startDate : null, endDate : null}, 
        {startDate : {$gte : new Date()}, endDate : null}, 
        {startDate : {$gte : new Date()}, endDate : {$lte : new Date()}}, 
        {startDate : null, endDate : {$lte : new Date()}}
       ]}

The real problem here is that I already use an $or condition for other restrictions, making it even more complex.

What would be your options to solve this apparently very simple problem ?


Implement some logic inside your application code in order to specify the right query based on the parameter constraints instead of trying to resolving this issue directly on the MongoDB query layer. The query language of MongoDB is partly too weak in this case.


I think you need this: https://jira.mongodb.org/browse/SERVER-1089.

0

精彩评论

暂无评论...
验证码 换一张
取 消