"pmsStatus": [
{
status: 0,
fromTimeStamp: "12:40:50",
reason: "no crane",
toTimeStamp: "13:40:50"
},
{
status: 1,
fromTimeStamp: "13:40:50",
toTimeStamp: "14:40:50"
},
{
status: 0,
fromTimeStamp: "14:40:50",
toTimeStamp: "15:40:50",
reason: "breakfast",
}
]
In, consider this as a mongodb collection. And I need to make findOneAndUpdate operation on this. Not on the same status, but status should be different. If the same status we get means, it should not perform any update operation. If the status is different from previous means it should perform update operation. If status we get to store into collection is 1 and the status of previous last element of object is 0, then toTimeStamp should be added wth current time to last object. And for newly stored object's status is 0 and fromTimeStamp should be added with current time(i,e) the toTeStamp of last element and FromTimeStamp of current element is always same
My code
pmsStatus.findOneAndUpdate(
{_id}
, {
$push: {
dataP
}
})
I know the above code is not proper, but please help me. I'm a beginner to mongodb. Thank you