分享

MongoDB如何查询固定时间?

linjikai8888 发表于 2015-12-15 09:23:59 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 11279
先贴测试代码!
        public Date queryBeforeWeek(String routeId, Date d) throws Exception {
               
                Query<BayonetFlowHis> q = ds.createQuery(BayonetFlowHis.class);
                //q.and(q.criteria("routeId").equal(routeId),q.criteria("DateTime").equal(d));
                q.filter("routeId", routeId);
                q.field("DateTime").equal(d);
                return q.asList().get(0).DateTime;
        }


只要把q.field("DateTime").equal(d);注释掉,就可以成功返回结果。用filter方法也是一样!


不注释的话,logs就直接返回
2015-12-15 09:07:56.877 [org.mongodb.driver.protocol.query] [main] [DEBUG] - Sending query of namespace warningFlow.BAYONET_FLOW_HIS on connection [connectionId{localValue:2, serverValue:423}] to server 172.5.1.94:27017
2015-12-15 09:07:56.962 [org.mongodb.driver.protocol.query] [main] [DEBUG] - Query completed

mongodb用morphia框架具体如何让时间字段等于某个固定时间来查询呢?


已有(2)人评论

跳转到指定楼层
langke93 发表于 2015-12-15 18:22:23
楼主使用find试试,参考下面
Query q = ds.createQuery(Person.class).field("addresses.city").equal("San Francisco");  
//or with filter, or with this helper method  
Query q = ds.find(Person.class, "addresses.city", "San Francisco");

回复

使用道具 举报

linjikai8888 发表于 2015-12-24 15:33:42
忘了回复了,

问题后面找到了,是由于写数据时间的时候。

mongo时区是多加8个小时 还是少8个小时了。。

总之就是由于这个时间差异,导致我用时间去查找的时候,就找不到数据了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条