Getting elements in a specfic index range of nested array

Hi there,

I have a golang project, which uses mongo driver (“go.mongodb.org/mongo-driver/mongo”).
My collections live in Atlas free tier cluster. They have a collection like below.
I want to filter documents by company name and get elements in a specific index range of the nested array “employees”. (For example, getting only first and second employees who works for ABC company)
How can I do that by Mongo query and golang driver?

[
    {
        "_id": "hoge",
        "companyname": "ABC",
        "empoloyees": [
            {
                "name": "John Doe",
                "email": "john@ABC.com"
            },
            {
                "name": "Jone Doe",
                "email": "jone@ABC.com"
            }
        ]
    },
    {
        "_id": "fuga",
        "companyname": "XYZ",
        "empoloyees": [
            {
                "name": "Tom Cat",
                "email": "john@XYZ.com"
            },
            {
                "name": "Dazy Dog",
                "email": "jone@XYZ.com"
            }
        ]
    }
]

Hello @Ryo_Koezuka, welcome to the MongoDB Community forum!

You can use the $slice projection operator to limit the number of elements in the query result. Here is an example post from this forum: Limit the items returned from array

Hello @Prasad_Saya , thx for reply!
Actually, I tried slice projection in golang some days ago, but I saw a error saying something like Mongo Atlas free tier users cannot use slice projection…
Is that expected behavior?

I don’t know. You can refer to this for clarification:

@Prasad_Saya
OK, Thanks a lot!

There are not reasons why such a simple operation as $slice is not supported on free tier.

Most likely you are not using it correctly.

Share your code and we will be able to pin point your error.