I am trying to write a query like:
var query = {
userId: req.params.userId,
isActive: true,
'interval.start': {
$lte: now
},
'interval.end': {
$or: [
{ $gte: now },
{ $exists: false },
{ $eq: null }
]
}
};
What I am trying to achieve is I want to find columns that should fit to these conditions:
I am getting: Can't use $or with Date.
error.
Thank you.
Here's how you declare a path of type Date with a Mongoose schema: const mongoose = require('mongoose'); const userSchema = new mongoose. Schema({ name: String, // `lastActiveAt` is a date lastActiveAt: Date }); const User = mongoose.
The Date SchemaType configures MongoDB to store the date as a BSON native Date object in our database. An example is shown below: ISODate("1990-01-01T23:00:00Z") The example above shows how Date is stored in a MongoDB database.
Timestamps save the current time of the document created and also when it was updated in form of a Date by turning it true. When set to true, the mongoose creates two fields as follows: createdAt: Date representing when the document was created. updatedAt: Date representing when this document was last updated.
Basically you have two options: Save time in type: String. Save date time in type: Date.
As far as I know you can't nest $or inside some specific field. The $or operator takes an array of expressions.
Try the following query:
var query = {
userId: req.params.userId,
isActive: true,
'interval.start': {
$lte: now
},
$or: [
{'interval.end': {$gte: now} },
{'interval.end': {$exists: false} },
{'interval.end': null }
]
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With