How to return mongodb in specific json layout? -


i have test data querying using java mongodb client. familiarizing myself platform , have been been able run basic queries.

public class returnjson {      public static void main(string[] args) {          try {             mongoclient mongoclient = new mongoclient("localhost", 27017);             db db = mongoclient.getdb("test");              dbcollection table = db.getcollection("zips");              basicdbobject searchquery = new basicdbobject();             searchquery.put("pop", new basicdbobject("$gt", 25000).append("$lt", 26000));              dbcursor cursor = table.find(searchquery);              while (cursor.hasnext()) {                 system.out.println(cursor.next());             }             cursor.close();          } catch (unknownhostexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

however, turn standard output java client gives me..

{     "city": "huntsville",     "loc": [         -86.567318,         34.726866     ],     "pop": 25513,     "state": "al",     "_id": "35801" } {     "city": "montgomery",     "loc": [         -86.243394,         32.383443     ],     "pop": 25282,     "state": "al",     "_id": "36109" } 

into bits need, i.e.

{      "city" : "huntsville" ,      "pop" : 25513  }  {     "city" : "montgomery" ,     "pop" : 25282  } 

i have seen bson , mongojack, not sure if need.

the plan me make information available via rest service, question later on.

many thanks.

ok, after digging, found needed this...

    basicdbobject searchquery = new basicdbobject();     basicdbobject fields = new basicdbobject();      searchquery.put("pop", new basicbsonobject("$gt", 25000).append("$lt", 25100));     fields.put("city", 1);     fields.put("pop", 1);     fields.put("_id", 0);      dbcursor cursor = table.find(searchquery, fields); 

this returned...

{ "city" : "fort ord" , "pop" : 25009} { "city" : "lakewood" , "pop" : 25008}

which needed.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -