I have a schema like this
{
_id: ObjectId('xyz'),
project_type: 'A', // enum ['A', 'B']
status: 'Working', // enum ['Pending', 'Working', 'Completed']
userId: ObjectId('abc'),
work_start: [{ userId: ObjectId('pqr'), timestamp: Date() }],
work_submitted: [{ userId: ObjectId('pqr'), timestamp: Date() }]
}
I want to fetch the document based on the following condition
and while fetching I also want to update the work_start array with a new object
Here is the query I have written but I am not able to fulfill the 2nd condition
collection.findOneAndUpdate(
{
project_type: 'A',
$or: [
{ status: 'Pending' },
{
$and: [
{ status: 'Working' },
{ userId: userId }
]
}
]
},
{
$push: { work_start: { userId, timestamp: new Date() } }, // Here I want to use the conditional block
$set: {
status: 'Working',
userId: userId
}
},
{
sort: { "created_on": 1 },
returnDocument: 'after' // return updated document
},
);
How can I do something like this?
$cond: {
if : { status: 'Working' and {userId: userId}},
then: // do nothing
else: { $push: { work_start: { userId, timestamp: new Date() } } } }, }