Let’s say I have the following data model in Mongo:
{
_id: ...,
name: "...",
obj: {...},
list: [ ... ],
}
Now let’s say, my list array is very long, and I don’t want to grab the whole document every time. So I want to get obj and name, but only grab the last 5 elements in list. How do you do this with with Mongo? I’m using pymongo.
I think you are looking for the
$sliceoperator. Docs are here.The syntax you are looking for is something like this:
Note that this will also return the
_idfield by default. If you do not want the_idadd_id:0in front ofobj:1. This is the JS syntax, but the python syntax will be very close.