Hi,
I’m working on a project where I need to be able to conditionally add data if the start date is greater than the end dates saved in the database or if the end date is less than the start dates saved in the database.
example of data:
_id: ''
appointments: [
{
_id: '',
location: '',
dateTimeStamp: [ 1689952800000, 1689954600000],
},
]
I’ve tried achieving this by:
const appointmentsExist = await Appointments.findOneAndUpdate({_id:user._id},
[
{
$set: {
appointments:{
$cond: {
if: {
$or:[
{ $lt: [
{ $ifNull: [ "$appointments.dateTimeStamp.1", 0 ] }, args.input.appointments[0].dateTimeStamp[0]
]
},
{ $gt: [
{ $ifNull: [ "$appointments.dateTimeStamp.0", 0 ] }, args.input.appointments[0].dateTimeStamp[1]
]
},
],
},
then: {$concatArrays:[{$ifNull: ["$appointments", []]}, args.input.appointments]},
else: {}
}
}
}
}
],{upsert: true})
where args.input.appointments
(example) is:
[{
location: '',
dateTimeStamp: [ 1689952900000, 1689954900000],
}]
The problem that I’m having is that the array of args.input.appointments
is being pushed to the database whether $appointments.dateTimeStamp.1
< args.input.appointments[0].dateTimeStamp[0]
or $appointments.dateTimeStamp.1
> args.input.appointments[0].dateTimeStamp[0]
, and the same is true for $appointments.dateTimeStamp.0"
> args.input.appointments[0].dateTimeStamp[1]
or $appointments.dateTimeStamp.0"
< args.input.appointments[0].dateTimeStamp[1]
.
Does anyone know why this is happening or what I’m doing wrong? I’ve even tried testing a simpler version in mongoplayground where dateTimeStamp
is only a number value instead of an array, and I still am getting similar issues. I would really appreciate any help. Thank you!