Google Groups

Re: [mongodb-user] $slice limit must be positive


Scott Hernandez Apr 9, 2012 6:08 AM
Posted in group: mongodb-user
Your syntax seems a little off, and is the reason the error message is
generated. If you just want the last two you don't use an array as the
value for $slice, which it seems you have:

> db.arrays.find({},{a:{$slice:-1}})
{ "_id" : 1, "a" : [ 3 ] }
> db.arrays.findOne()
{ "_id" : 1, "a" : [ 1, 2, 3 ] }

> db.arrays.find({},{a:{$slice:[-1,0]}})
error: { "$err" : "$slice limit must be positive", "code" : 13100 }

Please provide your code if this doesn't make sense, or solve your problem.

On Mon, Apr 9, 2012 at 7:03 AM, SID <sid.24....@gmail.com> wrote:
> Hi,
>
> I am unable to use negative integer as slice limit. I have set the
> offset = 0 and limit = -2 to fetch the last 2 records of an array
> field. Java driver throws below exception.
>
> org.springframework.data.mongodb.UncategorizedMongoDbException: $slice
> limit must be positive; nested exception is
> com.mongodb.MongoException: $slice limit must be positive"
>
> This is evident from assertion 13001 in projection.cpp
>
> else if (e2.type() == Array) {
>                        BSONObj arr = e2.embeddedObject();
>                        uassert(13099, "$slice array wrong size",
> arr.nFields() == 2 );
>
>                        BSONObjIterator it(arr);
>                        int skip = it.next().numberInt();
>                        int limit = it.next().numberInt();
>                        uassert(13100, "$slice limit must be
> positive", limit > 0 );
>                        add(e.fieldName(), skip, limit);
>
>                    }
>
> Documentation says that using -ve integer will return the results from
> the tailend of the array.
>
> Any pointers to overcome this limitation?
>
> Thx,
> Sid
>
> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>