您好我想使用java查询MONGO。
我有如下查询:
db.flights.find({“timestamp”:{“$ lte”:new Date(“2014-09-05T00:00:00.001Z”)}})。count()
其中输出为68
当我尝试使用如下的java代码时:
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);
输出为48。
有人可以告诉我代码中的问题吗?
答案 0 :(得分:0)
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 your class name..className
if(classNames.size()>0)
classObject=classNames.get(0);