$slice limit must be positive

629 views
Skip to first unread message

SID

unread,
Apr 9, 2012, 7:03:02 AM4/9/12
to mongodb-user, ssh...@saba.com
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

Scott Hernandez

unread,
Apr 9, 2012, 9:08:21 AM4/9/12
to mongod...@googlegroups.com
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.

> --
> 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.
>

SID

unread,
Apr 10, 2012, 7:52:14 AM4/10/12
to mongod...@googlegroups.com
Hi Scott,

Thanks, it worked!

-Sid

> To unsubscribe from this group, send email to mongodb-user+unsubscribe@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages