1

Here's what my document looks like.

{
    ID: "XYZ",
    messages: [
        {
            ID: "AAA",
            text: "this is a message"
        },
        {
            ID: "BBB",
            text: "this is a message"
        },
        {
            ID: "CCC",
            text: "this is a message"
        },
        {
            ID: "DDD",
            text: "this is a message"
        },
        {
            ID: "EEE",
            text: "this is a message"
        },
        {
            ID: "FFF",
            text: "this is a message"
        },
        {
            ID: "GGG",
            text: "this is a message"
        }
    ]
}

And I want to aggregate and get messages for example from CCC to FFF

{
    ID: "XYZ",
    messages: [
        {
            ID: "CCC",
            text: "this is a message"
        },
        {
            ID: "DDD",
            text: "this is a message"
        },
        {
            ID: "EEE",
            text: "this is a message"
        },
        {
            ID: "FFF",
            text: "this is a message"
        }

    ]
}

So how is this possible?

3
  • Try using $unwind, $match, and then $group in your pipeline. Commented Apr 14, 2013 at 0:44
  • I already did, that's not enough for this problem Commented Apr 14, 2013 at 1:16
  • Can you show what you tried? Commented Apr 14, 2013 at 1:18

1 Answer 1

2

This can be achieved by using the $slice operator. Check out the mongodb documentation on $slice. Run your query to get the document and use the slice operator with the query:

query.slice('messages', [numOfElementsToSkip, numOfElementsToReturn]);

Or more specific to your request:

query.slice('messages', [2, 4]); // Return 4 elements after skipping 2
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.