2

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.

2
  • the last you mean the latest record added to sampleStatus, right ? Commented Apr 2, 2017 at 5:06
  • Yeah @thelonglqd, it is the last one. Commented Apr 3, 2017 at 3:55

1 Answer 1

1

You can do this with the aggregation framework

db.collection.aggregate([
    { "$match": { "correlativeCode": "CSLLPA53E20M017W" } },
    { "$redact": { 
        "$cond": [
            { "$eq": [ 
                { "$let": {
                    "vars": { 
                        "item": { "$arrayElemAt": [ "$sampleStatus", -1 ] }
                    },
                    "in": "$$item.user"
                } },
                "user5"
            ] },
            "$$KEEP",
            "$$PRUNE"
        ]
    }}
])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.