Does the Java Driver support $slice operator?

794 views
Skip to first unread message

Stuart King

unread,
Mar 3, 2011, 1:55:56 PM3/3/11
to mongodb-user
I can't seem to find it in the source, or see a test to show it
working?

Is it supported in the Java driver? Is there an example usage?

Scott Hernandez

unread,
Mar 3, 2011, 2:10:41 PM3/3/11
to mongod...@googlegroups.com
It is supported, you just need to construct the DBObject correctly;
the driver just sends it on to the server, there is no specific
feature that is needed to be coded in the driver.

What does your code look like now?

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

Stuart King

unread,
Mar 3, 2011, 2:16:57 PM3/3/11
to mongodb-user
Thanks for the prompt reply.

From the console, my document is structured like this:

> db.principals.findOne()
{
"_id" : ObjectId("4d6fe8954542d242195344b2"),
"className" : "com.wenzani.document.principal.PrincipalsDocument",
"ctx_id" : "4d6fe8954542d242175344b2",
"dmn_id" : "4d6fe8954542d242165344b2",
"prncpal_ids" : [
"4d6fe8954542d242185344b2",
"4d6fe8954542d2421a5344b2",
"4d6fe8954542d2421b5344b2",
"4d6fe8954542d2421c5344b2",
"4d6fe8954542d2421d5344b2",
"4d6fe8954542d2421e5344b2",
"4d6fe8954542d2421f5344b2",
"4d6fe8954542d242205344b2",
"4d6fe8954542d242215344b2",
"4d6fe8954542d242225344b2"
],
"usr_id" : "4d6fe8954542d242155344b2"
}

I am using Morphia for the insertion (nice one Scott ;)) but the Java
Driver directly for the query, which looks like this:

BasicDBObject slice = new BasicDBObject("$slice", 1);
BasicDBObject principalsQuery = new BasicDBObject("prncpal_ids",
slice);
DBObject found = collection.findOne(principalsQuery);
System.out.println(found);

And I get this exception:

com.mongodb.MongoException: invalid operator: $slice
at com.mongodb.MongoException.parse(MongoException.java:53)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
at com.mongodb.DBCollection.findOne(DBCollection.java:480)
at com.mongodb.DBCollection.findOne(DBCollection.java:469)
at
com.wenzani.document.principal.PrincipalServiceImplTest.testSlice(PrincipalServiceImplTest.java:
210)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at org.junit.runner.JUnitCore.run(JUnitCore.java:130)
at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:
65)

Stu

Bernie Hackett

unread,
Mar 3, 2011, 2:17:33 PM3/3/11
to mongodb-user
You could do something like this:

BasicDBObject query = new BasicDBObject();
query.put("<array name>", new BasicDBObject("$slice", <number>));
cur = coll.find(query);

Lucas Zamboulis

unread,
Mar 3, 2011, 2:43:18 PM3/3/11
to mongod...@googlegroups.com, Bernie Hackett
@Stuart: The problem is that you have $slice as part of the selection,
whereas you should have it as part of the projection. So you should
modify your code to be as follows:

BasicDBObject projection = new BasicDBObject("prncpal_ids", new
BasicDBObject("$slice", 1));
BasicDBObject selection = new BasicDBObject("prncpal_ids",...);
DBObject found = collection.findOne(selection, projection);
System.out.println(found);

Mongo docs page:
http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields

Regards,
Lucas

Stuart King

unread,
Mar 3, 2011, 2:50:09 PM3/3/11
to mongodb-user
Thanks Lucas,

Just figured that out.

Cheers

Stu

Madhu Babu Badineni

unread,
Nov 4, 2015, 1:22:06 AM11/4/15
to mongodb-user, stuart...@gmail.com
BasicDBObject query1 = new BasicDBObject(); 
query1.put("responseQuestions", new BasicDBObject("$slice",1)); 
DBCursor cur = collection.find(query1); 
while (cur.hasNext()) 
System.out.println(cur.next()); 
i am getting the below error 
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar 
Exception in thread "main" com.mongodb.MongoException: Can't canonicalize query: BadValue unknown operator: $slice
at com.mongodb.MongoException.parse(MongoException.java:82)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:314)
at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:295)
at com.mongodb.DBCursor._check(DBCursor.java:368)
at com.mongodb.DBCursor._hasNext(DBCursor.java:459)
at com.mongodb.DBCursor.hasNext(DBCursor.java:484)
at com.aail.javatomongo.Test.main(Test.java:53)

Stephen Steneker

unread,
Nov 4, 2015, 4:17:10 AM11/4/15
to mongodb-user

Hi Madhu,

The original discussion you replied to was from 2011. Instead of replying to old discussions, in future please start a new discussion with your question including relevant details for your environment (eg. specific versions of MongoDB Java driver and MongoDB server).

It looks like your issue is actually the same as a few posts earlier in this same thread:
  https://groups.google.com/d/msg/mongodb-user/4c3P0_FOzyM/HDbVAptR5LsJ

You are trying to use the $slice operator in a query when it should instead be part of a projection (second parameter to find()).

Regards,
Stephen

Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages