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