[JAVA-1657] Java driver throws error with $query Created: 21/Feb/15 Updated: 11/Mar/15 Resolved: 22/Feb/15 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | Query Operations |
| Affects Version/s: | 2.12.5 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | sandip | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Hi , From the java driver when I run the attached query it throws following error com.mongodb.MongoException: Can't canonicalize query: BadValue unknown top level operator: $query The code and mongodb output is here: ) { "_id" : 1, "price" : 10 } { "_id" : 2, "price" : 20 } { "_id" : 3, "price" : 5 } { "_id" : ObjectId("54dadd1c63108d63b47e79c3") } { "_id" : ObjectId("54e23eb9e63b0d8071b5b2af") }{ "_id" : ObjectId("54e23ed3e63b0d8071b5b2b0") ================================================ DBCollection table = db.getCollection("test"); "); while (cursor.hasNext()) { System.out.println(cursor.next()); } |
| Comments |
| Comment by Jeffrey Yemin [ 11/Mar/15 ] | ||||||||||||||
|
Yes, the recommended way to specify a sort is via the sort method on DBCursor. Whether or not you choose to do that by parsing JSON is up to you. I did reproduce your results when connected to a mongos, and I don't have a hypothesis as to why it failed. | ||||||||||||||
| Comment by sandip [ 11/Mar/15 ] | ||||||||||||||
|
Thanks . So do you suggest to use "sort" with the way you updated ? In your case single mongo instance is effectively like a shard . But can you try connecting to shard and check what happens ? | ||||||||||||||
| Comment by Jeffrey Yemin [ 11/Mar/15 ] | ||||||||||||||
|
I can reproduce your results with a modified test program. I had to remove the array from the JSON string, as well as the projection, which was the second element of the array.
However, this works fine:
| ||||||||||||||
| Comment by Jeffrey Yemin [ 10/Mar/15 ] | ||||||||||||||
|
I connected to a standalone mongod, which is effectively like a shard. | ||||||||||||||
| Comment by sandip [ 10/Mar/15 ] | ||||||||||||||
|
Have you tried connecting directly to the shard ? The exception will not be thrown . Lets assume the shard is running on 27019 , instead of connecting to default 27017 connect to shard to 27019 | ||||||||||||||
| Comment by Jeffrey Yemin [ 10/Mar/15 ] | ||||||||||||||
|
I'm not seeing the same results as you. Running this simplified sample program:
I get an exception when connected to a mongod:
| ||||||||||||||
| Comment by sandip [ 09/Mar/15 ] | ||||||||||||||
|
I have found something interesting • Case 1: If we connect to mongos and try to execute the above code , we get an exception ( described in email below) Is this also expected? | ||||||||||||||
| Comment by Jeffrey Yemin [ 23/Feb/15 ] | ||||||||||||||
|
The driver is not built in a way that allows you to do this, nor is the MongoDB wire protocol itself. You will have to pull out the pieces from the user-supplied query and apply them to the DBCursor in your own code. | ||||||||||||||
| Comment by sandip [ 23/Feb/15 ] | ||||||||||||||
|
Hi , How can we try the orderby query . The orderby syntax inside java program looks like DBObject searchQuery = (DBObject)JSON.parse("[{$query:{},$orderby:{'stu_id':1}},{_'id':0}]"); Our system is like this . The user provides a query as input and we take the input on send the query to mongodb for return result . So the above one you have mentioned will work . But what for the orderby queries ? Internally we dont change the query , just pass it as the way it is entered as input. Thanks | ||||||||||||||
| Comment by Jeffrey Yemin [ 22/Feb/15 ] | ||||||||||||||
|
The server generates this error because $query is not a query operator. If you're just trying to search for documents where price is 10, easiest way is:
|