0

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

6
  • 1
    Are you sure, that your JSON is valid? Commented Jul 29, 2014 at 12:23
  • In a valid JSON file everything must be wrapped in quotes. Maybe that's the problem. Commented Jul 29, 2014 at 12:31
  • @DmitryTsechoev this JSON is valid and workd in mongoshell. Commented Jul 29, 2014 at 13:07
  • @morkro quotes aren't the problem when exceuting the query in JAVA Commented Jul 29, 2014 at 13:08
  • The JSON is invalid. There are two objects which are neither values of a key, nor elements of an array. See json.org for details about the JSON standard. Commented Jul 29, 2014 at 13:41

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Why dont you try something like this

String json = "{'type':'exam','score':1,'_id':0}";
DBObject dbObj = (DBObject) JSON.parse(json);

It properly ends up in the result as you expect,

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

The Reason is obviously, it cannot parse the String, It can parse only JSON.

As we see both your idea and this ends up in the same query framing

4 Comments

Writing the JSON as u did didn't solved my issue. I still have an empty List<DBObject> when I do a find.
The query is not about displaying all exam's with score 1 and _id 0. This is about displaying only score field.
Ismail, I know its a sample json, So give me your sample db data and send us the query you framed. And by the tell us what you expect from the db data.
Could you please see the Edits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.