mongodb - Matching mongo query using Java -
hi want query mongo using java.
i have query below:
db.flights.find({"timestamp" : {"$lte": new date("2014-09-05t00:00:00.001z")}}).count()
which gives output 68
when try using java code below:
table = db.getcollection("flights"); dbobject match = new basicdbobject(); date enddate = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sss").parse("2014-09-05t00:00:00.001z"); match.put("timestamp", new basicdbobject("$lte", enddate)); int count=table.find(match).count(); system.out.println(collections+" "+"count : "+count);
the output 48.
can tell me issue in code ??
saving format dateformat format = new simpledateformat("yyyy-mm-dd hh:mm:ss"); dateformat formatdate = new simpledateformat("yyyy-mm-dd"); classname classobject=null; date d1=new date(); classobject.setdatecolumn(format.parse(format.format(d1))); /---------------------------------------------------/ checking format date d1=new date(); date startdate=format.parse((formatdate.format(d1)+" 00:00:00")); date enddate=format.parse((formatdate.format(d1)+" 23:59:59")); classobject=classnameservice.classobjectcount(startdate,enddate); /---------------------------------------------------/ service method import org.springframework.data.mongodb.core.mongotemplate; import org.springframework.data.mongodb.core.query.query; @autowired(required=true) private mongotemplate mongotemplate; classname classobject=null; query query = new query(); query.addcriteria(org.springframework.data.mongodb.core.query.criteria.where("testid").in(testid)); query.addcriteria(org.springframework.data.mongodb.core.query.criteria.where("testdate").gte(startdate).lte(enddate)); list<classname> classnames=mongotemplate.find(query, classname.class);//put class name..classname if(classnames.size()>0) classobject=classnames.get(0);
Comments
Post a Comment