Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-1657

Java driver throws error with $query

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.12.5
    • Component/s: Query Operations
    • Labels:
      None

      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
      at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214)
      at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
      at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
      at com.mongodb.QueryResultIterator.<init>(QueryResultIterator.java:64)
      at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:86)
      at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
      at com.mongodb.DBCursor._check(DBCursor.java:458)
      at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
      at com.mongodb.DBCursor.hasNext(DBCursor.java:571)
      at aap.main(aap.java:57)

      The code and mongodb output is here:
      mongos> db.test.find({$query:{}},

      {"price":10}

      )

      { "_id" : 1, "price" : 10 } { "_id" : 2, "price" : 20 } { "_id" : 3, "price" : 5 } { "_id" : ObjectId("54dadd1c63108d63b47e79c3") } { "_id" : ObjectId("54e23eb9e63b0d8071b5b2af") }

      { "_id" : ObjectId("54e23ed3e63b0d8071b5b2b0")

      ================================================
      java Code:
      MongoClient mongo = new MongoClient("<Hostname>", 27017);
      DB db = mongo.getDB("test");

      DBCollection table = db.getCollection("test");
      DBObject searchQuery = (DBObject)JSON.parse("{$query:{}},

      {'price':10}

      ");
      DBCursor cursor = table.find(searchQuery);

      while (cursor.hasNext())

      { System.out.println(cursor.next()); }

            Assignee:
            Unassigned Unassigned
            Reporter:
            sannandi sandip
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: