Using mongoose. The Schema looks similar to this:
{
"name" : String,
"start" : Date,
"stop" : Date
}
What I'm trying to do, is to build a query, that ("AND"):
On the other hand, start and stop can be passed, or can be not passed (any of them).
All my attempts for while finished with some kind of Mongo Error :) So asking for some help.
Regards.
Your AND query logic can be expressed in either of the two ways :
Implicit AND operation:
Model.find({
"name": name,
"start": {
"$or": [
{ "$exists": false },
{ "$gte": start }
]
},
"stop": {
"$or": [
{ "$exists": false },
{ "$gte": stop }
]
}
}).exec(callback);
Explicit AND operation:
Model.find({
"$and": [
{ "name": name },
{
"start": {
"$or": [
{ "$exists": false },
{ "$gte": start }
]
}
},
{
"stop": {
"$or": [
{ "$exists": false },
{ "$gte": stop }
]
}
}
]
}).exec(callback);