mongodb - java get field value -


{ "_id" : 0, "cityname" : "tallinn", "jan" : -3 }  { "_id" : 1, "cityname" : "beijing", "jan" : -5 }  { "_id" : 2, "cityname" : "berlin", "jan" : 12 }  { "_id" : 3, "cityname" : "buenose", "jan" : 23 } 

i want calculate distance between tallinn other cities , beijing other cities continue entire document

this java code

try {     basicdbobject query = new basicdbobject();     basicdbobject select = new basicdbobject();     select.put("jan",1);     select.put("_id",1);     dbcursor cursor = coll.find(query,select);     basicdbobject obj = (basicdbobject)cursor.next();     int m,id;     id=integer.parseint(obj.getstring("_id"));     (int j=0;j<4;j++){         m= integer.parseint(obj.getstring("jan"));             if (id==j){                 while (cursor.hasnext()) {                     int ma;                     basicdbobject object = (basicdbobject)cursor.next();                     ma= integer.parseint(object.getstring("jan"));                     system.out.print((m-ma)+"  ");                 }         }     } } catch (mongoexception e){     system.out.println(e.getclass().getcanonicalname()); } 

output

 2 -9 -26 

i expect type of output

0 2 -15 -26 -2 0 -17 -28 15 17 0 -11 26 28 11 0 

first should find out distinct cityname , find jan of given city iterate on data , subtract value.

check below code :

 mongo mongo = new mongo("localhost", 27017);  db db = mongo.getdb("dbname");  dbcollection collection = db.getcollection("collectionname");  list distinctcity = collection.distinct("cityname");  for(int = 0; < distinctcity.size(); i++) {    basicdbobject query = new basicdbobject();    query.put("cityname", distinctcity.get(i));    basicdbobject project = new basicdbobject();    project.put("jan", 1);    project.put("_id", 0);    dbcursor cursordoc = collection.find(query, project);    while(cursordoc.hasnext()) {      basicdbobject object = (basicdbobject) cursordoc.next();      integer currentvalue = object.getint("jan");      dbcursor alldata = collection.find(new basicdbobject(), project);      while(alldata.hasnext()) {        basicdbobject alldataobject = (basicdbobject) alldata.next();        integer alldatajanvalue = alldataobject.getint("jan");        integer result = currentvalue - alldatajanvalue;        system.out.print("  " + result + "  ");      }      system.out.println();    }  }  } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -