Mongodb use slice and elementmatch
<p>I am trying to retrieve elements in an array in mongo db. I would like to retrieve the 15 first elements which do not match a pattern</p>

<p>So let's imagine I have</p>

<pre><code>{
"_id" : ObjectId("s4dcsd5s4d6c54s6d"),
"items" : [
    {
        type : "TYPE_1",
        text : "blablabla"
    },
    {
        type : "TYPE_2",
        text : "blablabla"
    },
    {
        type : "TYPE_3",
        text : "blablabla"
    },
    {
        type : "TYPE_1",
        text : "blablabla"
    },
    {
        type : "TYPE_2",
        text : "blablabla"
    },
    {
        type : "TYPE_1",
        text : "blablabla"
    }
]
}
</code></pre>

<p>So currently I have more element to match compared to the element to not match that's why I use nin. but it is to simplifiy</p>

<p>If I use </p>

<pre><code>db.history.find({ "_id" : ObjectId("s4dcsd5s4d6c54s6d")}, { "items" : { "$elemMatch" : { "type" : { "$nin" : [ "TYPE_2" , "TYPE_3"]}}}, "items" : { $slice : [0, 2]}}).pretty()
</code></pre>

<p>It seems that the element match is not taken into account (inverse as well if i put element match after slice)</p>

<p>Then if I do:</p>

<pre><code>db.history.find({ "_id" : ObjectId("s4dcsd5s4d6c54s6d")}, { "items" : { "$elemMatch" : { "type" : { "$nin" : [ "TYPE_2" , "TYPE_3"]}}, $slice : [0, 2]}}).pretty()
</code></pre>

<p>An error is thrown by mongo</p>

<p>Do you know how I can do?</p>

<p>Thanks a lot</p>