I have a collection of samples which is like this :
{
"_id" : ObjectId("58d6cbc14124691cd8154d72"),
"correlativeCode" : "CSLLPA53E20M017W",
"registrationMethod" : "taken",
"originPlace" : "INSPI",
"temperature" : 16,
"sampleStatus" : [
{
"nameStatus" : "status1",
"place" : "place1",
"rejectionReason" : "Nothing",
"user" : "user1",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status2",
"place" : "place2",
"rejectionReason" : "Nothing",
"user" : "user4",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status3",
"place" : "place3",
"rejectionReason" : "Nothing",
"user" : "user3",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status4",
"place" : "place4",
"rejectionReason" : "Nothing",
"user" : "user1",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
},
{
"nameStatus" : "status5",
"place" : "place5",
"rejectionReason" : "Nothing",
"user" : "user5",
"_id" : ObjectId("58d6cbc14124691cd8154d73")
}
]
}
First, I want to find a Sample by correlativeCode (it is very easy) but.. then I need to get the last element of sampleStatus and check if the user field of the sampleStatus array, is equal to an expression (another user).
The length of the sampleStatus array is variable.
I have tried $slice command but it doesn't work fine.
For example,
If I find a Sample with correlativeCode "CSLLPA53E20M017W" where the last sampleStatus.user is user1, it should fetch 0 records.
But if I find a Sample with correlativeCode "CSLLPA53E20M017W" where the last sampleStatus.user is user5, it should fetch 1 record.
Please help me, I'm stuck.
the lastyou mean the latest record added to sampleStatus, right ?