Search code examples
javajsonmongodbparsingmongo-java

com.mongodb.util.JSON.parse limit when parsing JSON and specifying projection


I'm using com.mongodb.util.JSON and parse method to query a mongodb database.

I'm facing an issue in a specific query where I want to choose specific fields to display.

The mongoshell query :

{{type:'exam'},{score:1,_id:0}}

In Java :

String query="{{type:'exam'},{score:1,_id:0}}";
DBObject dbObject=(DBObject) JSON.parse(query);
List<DBObject> dbOList=coll.find(dbObject).toArray();

The query should display only score field.

This return an empty dbOList.

I think that JSON.parse can't parse query that specify fields to display. Is that true ?

How can I parse thie kind of query ?

The application I'm developping is 100% dynamic, users can set queries they want, and I need to handle all possible cases !

I don't have a map, this is why I'm not using JONGO.

The JSON I want to parse can be more complex than this one given as an example, JSON.parse is perfect to parse nested JSON. But, I also need to handle specific fiedls query.

Am I missing something using JSON.parse from mongo-java-driver v2.12.2?

EDITS :

{"_id" : { "$oid" : "50906d7fa3c412bb040eb577" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb578" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb579" }, "student_id" : 0, "type" : "homework", "score" : 14.8504576811645 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57a" }, "student_id" : 0, "type" : "homework", "score" : 63.98402553675503 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57b" }, "student_id" : 1, "type" : "exam", "score" : 74.20010837299897 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57c" }, "student_id" : 1, "type" : "quiz", "score" : 96.76851542258362 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57d" }, "student_id" : 1, "type" : "homework", "score" : 21.33260810416115 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57e" }, "student_id" : 1, "type" : "homework", "score" : 44.31667452616328 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb57f" }, "student_id" : 2, "type" : "exam", "score" : 19.88180838833524 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb580" }, "student_id" : 2, "type" : "quiz", "score" : 1.528220212203968 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb581" }, "student_id" : 2, "type" : "homework", "score" : 60.9750047106029 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb582" }, "student_id" : 2, "type" : "homework", "score" : 97.75889721343528 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb583" }, "student_id" : 3, "type" : "exam", "score" : 92.6244233936537 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb584" }, "student_id" : 3, "type" : "quiz", "score" : 82.59760859306996 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb585" }, "student_id" : 3, "type" : "homework", "score" : 50.81577033538815 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb586" }, "student_id" : 3, "type" : "homework", "score" : 92.71871597581605 }
{ "_id" : { "$oid" : "50906d7fa3c412bb040eb587" }, "student_id" : 4, "type" : "exam", "score" : 87.89071881934647 }

The mongoshell query I'm trying to use in JAVA :

db.grade.find({type:exam},{score:1,_id:0})

This query display in the shell only score field as I want.

The problem is when I do it in JAVA using JSON.parse.

Ismail


Solution

  • Please understand how the mongo query works,

    As of mongodb is concerned, The query pattern and projection pattern are in this way,

    collection.find(query,projection); --> grade.find({type:exam,_id:0},{score:1});
    

    So It it obvious that, you need to separate query and projection part. So Give in the following way to execute your query as expected,

        String json = "{'type':'exam','student_id':0}";
        DBObject dbObj = (DBObject) JSON.parse(json);
        DBObject projection = (DBObject) JSON.parse("{'score':1,}");
        System.out.println(dbObj);
        Dao dao = new Dao();
        final DBCollection collection = dao.getDb("test").getCollection("test");
        **List<DBObject> dbOList=collection.find(dbObj,projection).toArray();**
        System.out.println(dbOList);
    

    This gives you the result as expected, So for sure you need to seperate the query and projection part as above.

    you will get the result as list of score field alone (with ID as default)

    [{ "_id" : { "$oid" : "53d8574e26b7d8cafbf80f9b"} , "score" : 54.6535436362647}]
    

    Thanks and Regards, Hari