I am trying to perform a query which is composed of two $or
's:
|--------------------
| Date1 | Date2 |
|--------------------
| NULL | NULL | *
| NULL | TODAY | *
| NULL | TOMRW |
| TODAY | TODAY | *
| TODAY | NULL | *
| TOMRW | NULL |
|--------------------
(I've marked the rows that would match with an asterisk)
(Date1 == null || Date1 <= today) && (Date2 == null || Date2 <= today)
I am not sure how to express this query in MongoDB.
It can be broken down into two individual queries that do exactly what they should:
{
"$or": [{
"Date1": {
"$exists": false
}
},
{
"Date1": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")
}
}]
}
and
{
"$or": [{
"Date2": {
"$exists": false
}
},
{
"Date2": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")
}
}]
}
Both of these select the correct set of documents - I just dont know how to execute them as a single query.
My initial thought was to do a query like this:
{
$and: [orQuery1, orQuery2]
}
Using an $and
query returns 0 results. It was explained why here in this thread: $and query returns no result
Also in that thread, a suggestion was made to do a query like this:
{
Key: {valToMatch1: 1, valToMatch2: 2}
}
But I dont think an $or
can be executed this way.
So, the question is: How do I construct my query such that I can combine the two $or's into a single query?
(Its getting very late so I hope this question makes sense.)
use test
db.test.insert({a:1})
db.test.insert({a:2, Date2:new Date("01/07/2012")})
db.test.insert({a:3, Date2:new Date("01/08/2012")})
db.test.insert({a:4, Date1:new Date("01/07/2012"), Date2:new Date("01/07/2012")})
db.test.insert({a:5, Date1:new Date("01/07/2012")})
db.test.insert({a:6, Date1:new Date("01/08/2012")})
first subquery
db.test.distinct('a', {...});
[1, 2, 3]
second subquery
db.test.distinct('a', {...});
[ 1, 5, 6 ]
(Date1 == null || Date1 <= today) && (Date2 == null || Date2 <= today)
unwind
Date1 == null && Date2 == null ||
Date1 == null && Date2 <= today ||
Date1 <= today && Date2 == null ||
Date1 <= today && Date2 <= today ||
query
db.test.find(
{
$or :
[
{$and: [
{"Date1": {"$exists": false}},
{"Date2": {"$exists": false}}
]},
{$and: [
{"Date1": {"$exists": false}},
{"Date2": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")}
}
]},
{$and: [
{"Date2": {"$exists": false}},
{"Date1": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")}
}
]},
{$and: [
{"Date2": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")}
},
{"Date1": {
"$exists": true,
"$lte": new Date("2012-01-07T04:45:52.057Z")}
}
]}
]
})
>[ 1 ]
this should work too (assume that 'not exist' and 'null' is the same)
db.test.find(
{
$and :
[
{$or: [
{"Date1": null},
{"Date1": { "$lte": new Date("2012-01-07T04:45:52.057Z")} }
]},
{$or: [
{"Date2": null},
{"Date2": { "$lte": new Date("2012-01-07T04:45:52.057Z")} }
]}
]
}
)
Actually, the problem lies not in the logic composition. Your nested $and
$or
query is perfectly fine.
It's the difference between MongoDB and JavaScript on creating a date that baffles you.
in Mongo:
> new Date("2012-01-07T04:45:52.057Z")
ISODate("0NaN-NaN-NaNTNaN:NaN:NaNZ")
in JavaScript:
> new Date("2012-01-07T04:45:52.057Z")
Sat, 07 Jan 2012 04:45:52 GMT
To fix it, you should use ISODate
in mongo.
> ISODate('2012-01-07T04:45:52.057Z')
ISODate("2012-01-07T04:45:52.057Z")
UPDATE: append tests:
to prove the correctness of your logic combination, I wrote the following scripts:
populate.js
: populate your sampling data:
var past = new Date('01/01/2011'),
future = new Date('01/01/2013');
var dates = [
{
num: 1
},
{
num: 2,
Date2: past
},
{
num: 3,
Date2: future
},
{
num: 4,
Date1: past,
Date2: past
},
{
num: 5,
Date1: past
},
{
num: 6,
Date1: future
}
];
dates.forEach(function(date) {
db.dates.insert(date);
});
query.js
: test the three queries:
function queryNum(query) {
print(db.dates.find(query).map(function(v) {
return v.num;
}));
}
var q1 = {
"$or": [{
"Date1": {
"$exists": false
}
},
{
"Date1": {
"$exists": true,
"$lte": new Date('01/01/2012')
}
}]
};
var q2 = {
"$or": [{
"Date2": {
"$exists": false
}
},
{
"Date2": {
"$exists": true,
"$lte": new Date('01/01/2012')
}
}]
};
var q3 = {
$and: [q1, q2]
};
queryNum(q1);
queryNum(q2);
queryNum(q3);
It properly prints:
1,2,3,4,5
2,2,4,5,6
1,2,4,5